在同一工作簿中使用for for loop编写多张纸
方案:
- 在输入处读取一个带有多张纸的Excel文件,
- 读取一些特定的列,并操纵现有数据
- 创建一个输出数据帧,
- 将此dataframe保存在一个现有输出文件中,因为
下面的新表是我的代码的片段:
for sheet in all_sheets: # all_sheets is the list of sheets from the input file
print(sheet) # Here i transform the data but removed the
writer = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
df_final_4.to_excel(writer, sheet_name=sheet, index=True)
writer.save()
问题语句:
- 文件仅包含一个包含最后一次迭代数据的纸。
- 以前的迭代被覆盖,并且在输出文件中只保留一张纸。
- 我尝试了多个批准,但是每个Yeild都相同的结果。
有人可以指导我出错的地方吗?
其他批准:
for sheet in all_sheets:
writer = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
df_final_4.to_excel(writer, sheet_name=sheet, encoding='utf-8', index=True)
writer.save()
for sheet in all_sheets[8:10]:
print(sheet)
writer = pd.ExcelWriter('newfile.xlsx', engine='xlsxwriter')
df_final_4.to_excel(writer, sheet_name=sheet, index=True)
writer.save()
wb = openpyxl.Workbook()
wb.save('Output.xlsx')
book = load_workbook('Output.xlsx')
writer = pd.ExcelWriter('Output.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df_final_4.to_excel(writer, sheet, index=True)
writer.save()
writer.close()
Scenario:
- Read an excel file with multiple sheets
- For each sheet at input read a few specific columns and manipulate the existing data
- Create an output dataframe
- Save this dataframe in one existing output file as a new sheet
Below is the snippet of my code:
for sheet in all_sheets: # all_sheets is the list of sheets from the input file
print(sheet) # Here i transform the data but removed the
writer = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
df_final_4.to_excel(writer, sheet_name=sheet, index=True)
writer.save()
Problem Statement:
- The file is written with only one sheet which contains the data for the last iteration.
- The previous iteration gets overwritten and only one sheet remains in the output file.
- I have tried multiple approches but each yeild the same result.
Can anyone please guide me where i am going wrong?
Other approches:
for sheet in all_sheets:
writer = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
df_final_4.to_excel(writer, sheet_name=sheet, encoding='utf-8', index=True)
writer.save()
for sheet in all_sheets[8:10]:
print(sheet)
writer = pd.ExcelWriter('newfile.xlsx', engine='xlsxwriter')
df_final_4.to_excel(writer, sheet_name=sheet, index=True)
writer.save()
wb = openpyxl.Workbook()
wb.save('Output.xlsx')
book = load_workbook('Output.xlsx')
writer = pd.ExcelWriter('Output.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df_final_4.to_excel(writer, sheet, index=True)
writer.save()
writer.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够通过更改代码的序列来解决问题:
这基本上是因为我们想在同一文件中写多张纸,因此作者的初始化或定义只能完成一次。
但是,如果只需要写一张纸,则可以在循环中的任何地方定义作者。
I was able to solve the issue by changing the sequence of the code:
This is basically because we want to write multiple sheets in the same file so the initialization or definition of the writer should be only done once.
However, in case, only one sheet is required to be written then the writer can be defined anywhere inside the loop.
我设法使用词典解决了这个问题。创建一个带有基本键的字典和以下代码地狱:
这创建了带有多个数据范围的新表。
I managed to solve this using dictionaries. create a dictionary with basic keys and the below code hellped:
this created new sheets with the multiple dataframes.