如何将多Excel工作簿与多个工作表相结合到单个工作簿中
我有3个带有单张纸的工作库。我需要将所有工作簿与3张床单相结合。
我尝试了以下代码:
from pandas import ExcelWriter
writer = ExcelWriter("Sample.xlsx")
for filename in glob.glob("*.xlsx"):
df_excel = pd.read_excel(filename,engine='openpyxl')
(_, f_name) = os.path.split(filename)
(f_short_name, _) = os.path.splitext(f_name)
df_excel.to_excel(writer, f_short_name, index=False)
writer.save()
我遇到了一个错误,例如“文件不是zip文件”
I have 3 workboooks with single sheets.I need to combine all workbooks into single workbook with 3 sheets.
I tried the below code :
from pandas import ExcelWriter
writer = ExcelWriter("Sample.xlsx")
for filename in glob.glob("*.xlsx"):
df_excel = pd.read_excel(filename,engine='openpyxl')
(_, f_name) = os.path.split(filename)
(f_short_name, _) = os.path.splitext(f_name)
df_excel.to_excel(writer, f_short_name, index=False)
writer.save()
i got an error like "File is not zip File"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“ sample.xlsx”是在与输入工作簿相同的目录中创建的,然后在使用
glob.glob(“*。xlsx”)
查找所有文件之前。因此,您尝试读取您的作家的“ sample.xlsx”。这不起作用。确保仅迭代真实的输入工作簿,例如这样:
更好的是将输出工作簿(“ sample.xlsx”)保存到另一个目录中,以避免混淆。显然,当您这样做时,您将无法再从列表中删除它,因此只需删除该行:
input_workbooks.remove(“ sample.xlsx”)
"Sample.xlsx" is created in the same directory as the input workbooks and before you look for all files with
glob.glob("*.xlsx")
. Therefore you try to read "Sample.xlsx" which is your writer. This isn't working.Make sure to only iterate over the real input workbooks e.g. like that:
Better would be to save the output workbook ("Sample.xlsx") to another directory to avoid confusion. Obviously, when you do that, you do can not remove it from the list any longer, so just delete the line:
input_workbooks.remove("Sample.xlsx")