Python操做Excel进行数据替换

近期有遇到了一个Excel的需求,因Excel文件太多,想着简化工做,经过Python进行数据处理。python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/3/26 15:13
# @Author : Zhanxing
# @Site : 
# @File : 修改.py
# @Software: 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。而个人数据是元组。不支持修改和判断。ide

因此在通过思考后,使用了“==”。excel

相关文章
相关标签/搜索