使用 xlwings 将 excel 个工作簿合并为一个

Merging excel workbooks into one using xlwings

我目前正在尝试将多个 excel 电子表格合并到一个工作簿中以创建月度主工作簿。

我已经编写了以下代码来尝试实现这一点:

...

from pathlib import Path
import xlwings as xw

print("enter file directory")
SOURCE_DIR = input()

excel_files = list(Path(SOURCE_DIR).glob("*.xlsx"))
combined_wb = xw.Book()

for excel_file in excel_files:
    wb = xw.Book(excel_files)
    for sheet in wb.sheets:
        sheet.api.copy(After=combined_wb.sheets[0].api)
    wb.close()

combined_wb.sheets[0].delete()
combined_wb.save(f("all_settlement_reports.xlsx"))

if len(combined_wb.app.books) == 1:
    combined_wb.app.quit()
else:
    combined_wb.close()

...

第一步很好,系统提示我输入文件路径,但随后出现以下错误,这让我很困惑:

Traceback (most recent call last):
  File "C:\Users\Callum\Desktop\env\AutoSettle.py", line 11, in <module>
    wb = xw.Book(excel_files)
  File "C:\Users\Callum\AppData\Local\Programs\Python\Python310\lib\site-packages\xlwings\main.py", line 817, in __init__
    fullname = fullname.lower()
AttributeError: 'list' object has no attribute 'lower'

任何人都可以帮忙解决这个问题,因为我真的很难解决这个问题。

谢谢大家

from pathlib import Path

import xlwings as xw

SOURCE_DIR = input("enter file directory")

excel_files = list(Path(SOURCE_DIR).glob("*.xlsx"))
combined = xw.Book()

app = combined.app
app.interactive = False
app.visible = False

reference_sheet = combined.sheets[0]
for file_name in excel_files:
    for sheet in xw.Book(str(file_name)).sheets:
        sheet.copy(before=reference_sheet)

reference_sheet.delete()
combined.save("all_settlement_reports.xlsx")
app.kill()