python - 如何使用 Xlwings 和 Python 合并同一 Excel 工作簿中的所有工作表

标签 python python-3.x excel xlwings

我正在尝试使用 Xlwings 合并同一 Excel 工作簿中的所有工作表,如果有人可以请建议如何完成此操作?

下面的代码能够获取所有工作表并将它们组合到创建的输出文件中,但工作表选项卡保持分离而不是合并。

import xlwings as xw
import glob
import sys

folder = sys.argv[1]
inputFile = sys.argv[2]
outputFile = sys.argv[3]

path = r""+folder+""

excel_files = glob.glob(path + "*" + inputFile + "*")   
with xw.App(visible=False) as app:
    combined_wb = app.books.add()
    for excel_file in excel_files:
        print(excel_file)
        wb = app.books.open(excel_file)
        for sheet in wb.sheets:
            sheet.copy(after=combined_wb.sheets[0])
        wb.close()
    combined_wb.sheets[0].delete()
    combined_wb.save(outputFile)
    combined_wb.close()

最佳答案


已更新**
更新了示例代码。
路径中每个工作簿中每个工作表的数据将粘贴到创建的组合工作簿中的默认工作表中。每个工作簿内容均由'row_separation' 行分隔。
工作簿中的每个工作表都从“A”列开始粘贴到合并工作表中,并由“col_separation”列分隔。
您需要确定将每个原始工作表粘贴到组合 Excel 文件中的 Sheet1 中的位置。

笔记;此复制/粘贴还应包括数据样式,如字体名称、颜色、大小和格式。

row_separation = 100
col_separation = 20

row = 1
with xw.App(visible=False) as app:
    combined_wb = app.books.add()
    for excel_file in excel_files:
        col = 1
        print("Reading Excel file: " + excel_file)
        wb = app.books.open(excel_file)
        for sheet in wb.sheets:
            ws = wb.sheets[sheet]
            wb_name = str(wb.name)
            sheet_name = str(sheet.name)
            print("Extracting data from " + wb_name + "-" + sheet_name)
            combined_wb.sheets[0].range(row, col).value = 'Data from Workbook: ' + wb_name + ' Sheet: ' + sheet_name
            combined_wb.sheets[0].range(row, col).font.bold = True
            ws.used_range.copy()
            combined_wb.sheets[0].range(row+1, col).paste(paste='all')
            col += col_separation
        wb.close()
        row += row_separation
    combined_wb.save(outputFile)
    combined_wb.close()

来自 3 个工作簿的示例工作表,工作簿 1 有 2 个工作表,工作簿 2 有 3 个工作表,工作簿 3 有 1 个工作表。显示时的行间距设置为 10,列间距设置为 8。



答案2 ----------------------------------
从相同的工作簿中选择相同的工作表并作为单独的工作表添加到组合_wb。

with xw.App(visible=False) as app:
    combined_wb = app.books.add()
    first_sheet = combined_wb.sheets[0]
    for excel_file in excel_files:
        col = 1
        print("Reading Excel file: " + excel_file)
        wb = app.books.open(excel_file)
        for sheet in wb.sheets:
            ws = wb.sheets[sheet]
            wb_name = str(wb.name)
            sheet_name = str(sheet.name)
            print("Extracting Sheet from " + wb_name + "-" + sheet_name)

            ws.api.Copy(After=first_sheet.api)
            first_sheet = combined_wb.sheets[sheet_name]

        wb.close()
    # combined_wb.sheets[0].delete()  # Delete initial Sheet1 if not required 
    combined_wb.save(outputFile)
    combined_wb.close()

enter image description here

关于python - 如何使用 Xlwings 和 Python 合并同一 Excel 工作簿中的所有工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73028056/

相关文章:

python - Pandas :从csv文件中获取特定列

python - 如何对列表中的连续重复项求和?

vba - 生成日期序列

python - 访问 r”C :\Windows\System32\Drivers\etc\hosts” 的权限被拒绝

python - 使用Python递归生成组合

Python ASCII 绘图仪

java - 如何将 HSSFWorkbook 转换为 CSV 文件..?

string - 如何确保我的 Excel 单元格将被视为字符串?

python - 如何在 Python 3 中转换/交叉表数据?

Python - 在字符串上打印变量