【发布时间】:2023-02-03 15:40:33
【问题描述】:
我的 Python 代码用于合并文件夹“excel_Report”中的多个 excel 文件 到 1 个主 excel 文件中。 我已经安装了所有库:pyodbc、pandas、plyer、glob2。 但是当我执行 python 时。有一个错误:
“NameError:名称‘文件名’未定义”
我不知道我的代码有什么问题。你能帮忙吗? 谢谢
import pyodbc
import pandas as pd
import os
from datetime import datetime
from plyer import notification
import glob
# getting excel files to be merged from the Desktop
path = "T:\excel_Report"
# read all the files with extension .xlsx i.e. excel
excel_files = glob.glob(path + "\*.xlsx")
print('File names:', filenames)
# empty data frame for the new output excel file with the merged excel files
outputxlsx = pd.DataFrame()
with xw.App(visible=False) as app:
combined_wb = app.books.add()
for excel_file in excel_files:
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("T:/excel_Report/test.xlsx")
combined_wb.close()
【问题讨论】:
-
您应该查看引发错误的行。您正在尝试
print('File names:', filenames),但filenames未定义。