【问题标题】:How to add multiple charts into different sheets of one excel?如何将多个图表添加到一个excel的不同工作表中?
【发布时间】:2020-02-29 13:55:40
【问题描述】:

我想将使用 xlsxwriter 创建的多个图表导出到一个 Excel 中的多个工作表中。我有两个数据框如下:

df_a = pd.DataFrame({'User':['101','102','103','104','105','106'],'CountA':[7,8,9,10,11,12],'CountB':[1,2,3,4,5,6],'CountC':[13,14,15,16,17,18]})

df_b = pd.DataFrame({'User':['107','108','109','110','111','112'],'ValA':[10,20,30,40,50,60],'ValB':[70,80,90,100,110,120],'ValC':[130,140,150,160,170,180]})

我已经成功创建了一个excel文件,即“test.xlsx”,其中包含df_a及其对应的堆积条形图,使用下面的代码:

#Create a Pandas Excel writer using XlsxWriter as the engine.
 excel_file_a = 'test.xlsx'
 sheet_name_a = 'testA'

 writer = pd.ExcelWriter(excel_file_a, engine='xlsxwriter')
 df_a.to_excel(writer, sheet_name=sheet_name_a,index=False)

#Access the XlsxWriter workbook and worksheet objects from the dataframe.
 workbook = writer.book
 worksheet_a = writer.sheets[sheet_name_a]

#Create a chart object.
 chart_a = workbook.add_chart({'type': 'column', 'subtype': 'stacked'})

#Configure the series of the chart from the dataframe data.
 for col_num in range(1, 4):
     chart_a.add_series({
         'name':       ['testA', 0, col_num],
         'categories': ['testA', 1, 0, 5, 0],
         'values':     ['testA', 1, col_num, 5, col_num],
         'gap':        2,
                         })

#Insert the chart into the worksheet.
 worksheet_a.insert_chart('G2', chart_a)

#Close the Pandas Excel writer and output the Excel file.
 writer.save()

但是,我还希望 df_b 及其各自的堆积条形图在同一个 Excel 文件“test.xlsx”中,但在不同的工作表中,比如说 sheetname = testB。

【问题讨论】:

    标签: python xlsxwriter


    【解决方案1】:

    这是使用循环的好地方。例如:

    # You only need one of each of these
    excel_file = 'test.xlsx'
    writer = pd.ExcelWriter(excel_file_a, engine='xlsxwriter')
    workbook = writer.book  
    dataframes = (df_a, df_b)
    
    # Loop through our list of dataframes
    for i in range(len(dataframes)):
        sheet_name = 'test' + 'abcdefghijklmnop'[i]    # testa, testb, etc.
        dataframe = dataframes[i]
    
        dataframe.to_excel(writer, sheet_name=sheet_name, index=False)
        worksheet = writer.sheets[sheet_name]
        ....
        # all of the other stuff down as far as...
        worksheet.insert_chart('G2', chart)
    
    # Now that we have finished our loop
    writer.save()
    

    【讨论】:

    • 嗨,我测试了你的方法,我只得到了 df_a 的结果。
    • ahh nvm,这是因为最后一个 writer.save() 上的缩进。立即获取,感谢您的帮助!
    【解决方案2】:

    我刚刚复制并粘贴了您的代码并更改了一些内容。 我不知道你的问题是什么,也许你覆盖了一些东西。

        excel_file_a = 'test.xlsx'
        sheet_name_a = 'testA'
        sheet_name_b = 'testB'
    
        writer = pd.ExcelWriter(excel_file_a, engine='xlsxwriter')
    
        df_a.to_excel(writer, sheet_name=sheet_name_a,index=False)
        df_b.to_excel(writer, sheet_name=sheet_name_b,index=False)
    
        workbook = writer.book
        worksheet_a = writer.sheets[sheet_name_a]
        worksheet_b = writer.sheets[sheet_name_b]
    
        chart_a= workbook.add_chart({'type': 'column', 'subtype': 'stacked'})
        chart_b= workbook.add_chart({'type': 'column', 'subtype': 'stacked'})
    
        for col_num in range(1, 4):
              chart_a.add_series({
                 'name':       ['testA', 0, col_num],
                 'categories': ['testA', 1, 0, 5, 0],
                 'values':     ['testA', 1, col_num, 5, col_num],
                 'gap':        2,
                                 })
    
        for col_num in range(1, 4):
              chart_b.add_series({
                 'name':       ['testB', 0, col_num],
                 'categories': ['testB', 1, 0, 5, 0],
                 'values':     ['testB', 1, col_num, 5, col_num],
                 'gap':        2,
                                 })
    
        worksheet_a.insert_chart('G2', chart_a)
        worksheet_b.insert_chart('G3', chart_b)
    
        writer.save()
    

    【讨论】:

    • 是的,最初我也尝试过这种方式,但我只得到了 df_a。我想我可能会覆盖一些东西。还是谢谢!
    猜你喜欢
    • 2018-07-03
    • 2015-11-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多