近期有遇到了一个Excel的需求,因Excel文件太多,想着简化工作,通过Python进行数据处理。#!/usr/bin/env python
# -*- coding: utf-8 -*-
[email protected]: 2020/3/26 15:13
[email protected]: Zhanxing
[email protected]:
[email protected]: 修改.py
[email protected]: PyCharm
import openpyxl
def run(excel_file, new_file):
wb = openpyxl.load_workbook(excel_file)
ws = wb.get_sheet_names()
print(ws)
for sheet in ws:
line = wb.get_sheet_by_name(sheet)
for row in line.iter_rows(min_row=2):
for cell in row:
if '点播' == cell.value:
line.cell(row=cell.row, column=cell.column, value='视频')
elif '点播短' == cell.value:
line.cell(row=cell.row, column=cell.column, value='视频')
elif '直播' == cell.value:
line.cell(row=cell.row, column=cell.column, value='视频')
elif '基础P' == cell.value:
line.cell(row=cell.row, column=cell.column, value='中间')
wb.save(excel_file)
if __name__ == '__main__':
run('111.xlsx', '333.xlsx')
在进行相关的编程过程中遇到很多的问题,下面一一做下记录。
对于判断过程中使用“in”进行判断,但是因“in”是在指定序列中找到值返回 True,否则返回 False。而我的数据是元组。不支持修改和判断。
所以在经过思考后,使用了“==”。