批量提取多个excel文件指定单元格内容,并汇总到一个新excel文件里

#-*- coding:utf-8 -*-
import os
import openpyxl
import pandas as pd
from pandas.core.frame import DataFrame
cellnames = ["A1","D10","L10","C11"]#指定文件单元格的位置
path = r"E:/test/" #目录路径
wb = openpyxl.Workbook()
nsh=[]
FileNames=os.listdir(path)
for fn in FileNames:
    fullfilename=os.path.join(path,fn)
    print(fullfilename)
    wb = openpyxl.load_workbook(fullfilename)
    sh = wb["sheet1"]
    arr = [sh[n].value for n in cellnames]
    wb.close()

    print(arr)
    nsh.append(arr)
    dataframe=pd.DataFrame(nsh)
    dataframe.columns=['A1','x','y','test1']#对应单元格读取的列名

dataframe.to_excel('E:/test/输出文件1.xlsx')#输出路径

你可能感兴趣的:(python,excel)