【问题标题】:Move sheets from one workbook to another in a loop在循环中将工作表从一个工作簿移动到另一个工作簿
【发布时间】:2020-05-23 06:29:21
【问题描述】:
import pandas as pd
xl_dict = {}
sheetname_list = ['2018', '2019', '2020']
#i am able to get the sheets from here
for sheet in sheetname_list:
    xl_dict[sheet] = pd.read_excel('Mvvvv Ds.xlsx', sheet_name=sheet)
    print(xl_dict[sheet].title)

无法在此文件中添加工作表,我想添加以将这三个工作表添加到此文件中。

path2 = '2020-01-29.xlsx'

【问题讨论】:

    标签: python excel python-3.x pandas xlsx


    【解决方案1】:

    我认为您应该查看Excel Writer。像下面这样的东西可能会起作用,

    writer = pd.ExcelWriter(path2)
    [xl_dict[sheetname_list[x]].to_excel(writer,sheet_name=sheetname_list[x]) for x in range(len(sheetname_list))]
    writer.save()
    

    【讨论】:

    • 现在试试上面的方法,xl_dict 必须通过键而不是索引来调用,这就是它给出错误的原因。现在应该可以工作了。
    • import pandas as pd xl_dict = {} sheetname_list = ['2018', '2019', '2020'] path2="A.xlsx" #i am able to get the sheet from here for sheet在 sheetname_list 中: xl_dict[sheet] = pd.read_excel('Mvv.xlsx', sheet_name=sheet) writer = pd.ExcelWriter(path2) [xl_dict[sheetname_list[x]].to_excel(writer,sheet_name=sheetname_list[x]) for x in range(len(sheetname_list))] writer.save()
    • 它尝试了上面的代码我收到错误 KeyError: '2019'
    猜你喜欢
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多