多个文件合并到同个文件同个sheet(追加)
from openpyxl import load_workbook, Workbook
import glob
path = "C:\\xiaoming"
new_workbook = Workbook()
new_sheet = new_workbook.active
flag = 0
for file in glob.glob(path + '/*.xlsx'):
workbook = load_workbook(file)
sheet = workbook.active
coloum_A = sheet['A']
row_lst = []
for cell in coloum_A:
if cell:
print(cell.row)
row_lst.append(cell.row)
if not flag:
header = sheet[1]
header_lst = []
for cell in header:
header_lst.append(cell.value)
new_sheet.append(header_lst)
flag = 1
for row in row_lst:
data_lst = []
for cell in sheet[row]:
data_lst.append(cell.value)
new_sheet.append(data_lst)
new_workbook.save(path + '/' + 'result.xlsx')
多个文件合并到同个文件不同sheet
import os
import pandas as pd
dir = "C:\\xiaoming"
origin_file_list = os.listdir(dir)
print(origin_file_list)
with pd.ExcelWriter('C:\\xiaoming\\result.xlsx') as writer:
for i in origin_file_list:
file_path = dir + '/' + i
sheet_name = i[:-4]
df = pd.read_excel(file_path)
string = "".join(list(str(i) for i in df.index))
if string.isdigit():
df.to_excel(writer, sheet_name, index=False)
else:
df.to_excel(writer, sheet_name)