本帖最后由 小恶芯 于 2022-2-19 14:43 编辑
使用方式:将mergeExcel.py和想要进行合并的.xlsx文件放入同一文件夹中,双击运行mergeExcel.py即可
按照文件名的升序排序进行合并,合并后的文件名:Output.xlsx
20220219更新支持多xlsx多Sheet合并,按照对应Sheet名称进行合并
import os
from openpyxl import Workbook
from openpyxl import load_workbook
def getAllFiles():
path = os.getcwd()
return [file for file in os.listdir(path)]
def isXlsx(file):
if os.path.splitext(file)[-1] == '.xlsx':
return True
else:
return False
def sortXlsx(files):
files.sort(reverse=False)
def getAllXlsx():
files = getAllFiles()
xlsxList = []
for file in files:
if isXlsx(file):
xlsxList.append(file)
else:
pass
return xlsxList
targetXlsx = 'Output.xlsx'
xlsxList = getAllXlsx()
sortXlsx(xlsxList)
wb = Workbook()
for xlsx in xlsxList:
wbtemp = load_workbook(xlsx)
for currentSheet in wbtemp.sheetnames:
wstemp = wbtemp[currentSheet]
max_row = wstemp.max_row
max_column = wstemp.max_column
if currentSheet not in wb.sheetnames:
ws = wb.create_sheet(currentSheet, -1)
else:
ws = wb[currentSheet]
if ws.max_row == 1 and ws.max_column == 1 and ws.cell(1, 1).value is None:
row = 0
else:
row = ws.max_row
for i in range(1, max_row + 1):
for j in range(1, max_column + 1):
data = wstemp.cell(i, j).value
ws.cell(row + i, j, data)
wb.save(targetXlsx)
示例图片:
合并前的工作簿1:
01
合并前的工作簿2的两张表:
02
03
合并后的工作簿中的两张表:
04
05
百度云链接:
链接:https://pan.baidu.com/s/1sZXj7Hb8VEXDkW2aHu7ApQ
提取码:52pj
--来自百度网盘超级会员V7的分享土豪下载链接:
mergeExcel Sup-Sheets Update220219.zip
(1.19 KB, 下载次数: 170)
|