1 概述
最近女票因为处理大量的Excel存在烦恼,我也学习一下如何处理Excel表格吧,顺便解决一下她的小问题
参考文章:
Python|读、写Excel文件(三种模块三种方式
python的openpyxl的使用笔记
openpyxl模块
2 需求分析
表格样式如下:
存在大量的excel表格,每个excel表格里面有大量的sheet页
- 如果姓名相同,但是身份证号不同,应该----错误告警
- 如果姓名不同,但是身份证号相同,应该----错误告警
- 如果姓名相同,身份证号也相同,就要把数字都加起来
3 环境搭建
由于比较熟悉Python,最后还是选择了python + openpyxl 的方式(pandas这个需要c++ 安装起来太费劲)
3.1 安装python
下载链接
下载完双击,默认安装即可
3.2 安装openpyxl
管理员打开cmd
执行以下命令:
pip install openpyxl
3.3 安装pycharm
最好安装个pycharm,有可能不知道怎么运行,或者修改个路径一类的(后面有时间,可以尝试用Python写个界面,支持可输入的)
4 脚本编写
写完了之后,大体就是这样子了,有点啰嗦,应该还可以优化:
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from openpyxl import load_workbook
import os
class ExcelUtil:
def __init__(self, read_path, save_path,column_dict):
self.read_path = read_path
self.save_path = save_path
self.column_dict = column_dict
def read_excel_sheet(self):
for excel_file in self.find_all_excel_file():
wb = load_workbook(excel_file)
for sheetnames in wb.sheetnames:
ws = wb[sheetnames]
self.process_sheet_data(ws)
file_name = excel_file.split(os.path.sep)[-1]
wb.save(self.save_path + os.path.sep + file_name)
def