最近發現excel數據量極大,而且經過簡單的數據操做不能提取到我須要的數據,若是單獨操做,數據量太大耗時太長。
想着經過簡單的方式,而且快速提取數據,就想到了Python。
python操做Excel使用的openyxl和pandas對Excel進行操做。
代碼以下:python
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020-02-24 下午 03:43 # @Author : Zhanxing # @Site : # @File : 提取字段.py # @Software: PyCharm import openpyxl import pandas as pd from xlutils.copy import copy df=pd.read_excel('222.xlsx',sheet_name='XXX') data=(df[['XXX','XXX']]) data.to_excel("new.xlsx", index=False) class Excel: def __init__(self, excel_file): self.excel = openpyxl.load_workbook(excel_file) self.sheet_name = self.excel.get_sheet_names() self.sheet = self.excel.get_sheet_by_name(self.sheet_name[0]) def excel_read(self): """ 返回excel每一行的生成器對象 :return: """ yield from self.sheet.iter_rows(min_row=2) def run(excel_file, new_excel): """ :param excel_file: 要處理的excel的路徑 :param new_excel: 處理後要保存的文件名 :return: """ excel = Excel(excel_file) excel_line = [line for line in excel.excel_read()] for line in excel_line: for cell in line: if isinstance(cell.value, int): continue people_name = cell.value.strip('[]').split(',') for num in range(len(people_name)): if 'XX' in people_name[num]: excel.sheet.cell(row=cell.row, column=cell.column, value=people_name[num]) break excel.excel.save(new_excel) if __name__ == '__main__': run('new.xlsx','11.xlsx')