【问题标题】:python xlsxwriter export dataframe and plotly graphs to different tabs in one excelpython xlsxwriter 导出数据框并将图表绘制到一个excel中的不同选项卡
【发布时间】:2020-09-10 06:31:07
【问题描述】:

我正在尝试将一些数据框和绘图导出到一个 Excel 中的不同选项卡中。每个选项卡应仅包含一个数据框或图形。我已经完成了数据框导出部分,但我不知道如何使用类似的逻辑导出绘图。

Xlsxwriter:导出两个数据框:table 和 misc_user

writer = pd.ExcelWriter(path_output + '\Report_{}.xlsx'.format(*arg), engine='xlsxwriter')

table.to_excel(writer, sheet_name='Uploads')
misc_user.to_excel(writer, sheet_name='Misc Users')

writer.save()

然后我有两个由其他两个数据框组成的绘图

# plotly 
user_evt_long = px.line(user_evt_long, x='Month', y='Times', color='ELEVATE?')

# Show plot 
user_evt_long.show()
top_users_fig = px.bar(top_users, x='account_name', y='users_count', title = 'Top Ten Uploads')

top_users_fig.show()

所以总共应该有四个标签。 “上传”标签包含table,“其他用户”标签包含misc_user,“用户”标签包含user_evt_long,“热门用户”包含top_users_fig

如何使用与数据框导出类似的逻辑导出user_evt_longtop_users_fig

【问题讨论】:

  • 您想在不同的选项卡中绘制 2 个图,对吗?或者您希望您的数据框也位于不同的选项卡中?所以你要找的总共有 4 个标签?或类似一个数据框,其中 1 个选项卡中的图形和其他数据框,其他选项卡中的其他图?
  • 我希望每个选项卡只包含一个数据框或图表。所以总共应该有四个标签。 “上传”选项卡包含“表”数据框,“其他用户”选项卡包含“misc_user”数据框,“用户”选项卡包含“user_evt_long”图表,“热门用户”包含“top_users_fig”图表。谢谢!

标签: python graph export xlsxwriter


【解决方案1】:

这是一个将 Plotly 图像添加到与 Pandas 数据框相同的工作表以及新工作表的示例。

import plotly.express as px
import pandas as pd
from io import BytesIO


# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_plotly.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Get the xlsxwriter workbook and worksheet objects.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

# Make a plotly chart.
fig = px.bar(df)

# Convert it to a BytesIO stream.
image_data = BytesIO(fig.to_image(format="png"))

# Write the image to the same sheet as the data.
worksheet.insert_image(2, 3, 'plotly.png', {'image_data': image_data})

# Create a new worksheet and add the image to it.
worksheet = workbook.add_worksheet()
worksheet.insert_image(2, 3, 'plotly.png', {'image_data': image_data})

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

输出

注意,也可以直接在 Xlsxwriter 中从 Pandas 数据帧中绘制图表。请参阅 XlsxWriter 文档的 Adding Charts to Dataframe output 部分。

【讨论】:

    【解决方案2】:

    请检查sn-p。您可以使用openpyxl 将图像和数据框插入到单独的选项卡中。每次您都需要提及您正在创建的选项卡名称。

    这只是我创建的一个示例。您可以根据您的要求对其进行修改。但它会根据您的需要创建 4 个标签。

    您需要先使用fig.write_image 保存您的图片,然后才能插入它。

    from openpyxl import Workbook
    from openpyxl.drawing.image import Image
    from openpyxl.utils.dataframe import dataframe_to_rows
    import plotly.graph_objects as go
    import plotly
    import pandas as pd
    
    data = [['Ravi',21,67],['Kiran',24,61],['Anita',18,46],['Smita',20,78],['Sunil',17,90]]
    df = pd.DataFrame(data,columns = ['name','age','marks'],dtype = float)
    trace = go.Bar(x = df.name, y = df.marks)
    fig = go.Figure(data = [trace])
    fig.write_image("Plotly1.png")
    
    wb = Workbook()
    sheet1 = wb.create_sheet('image1',0)
    active = wb['image1']
    active.add_image(Image('Plotly1.png'),'A1')
    
    sheet2 = wb.create_sheet('image2',0)
    active1 = wb['image2']
    active1.add_image(Image('Plotly1.png'),'A1')
    
    sheet3 = wb.create_sheet('marks',0)
    active2 = wb['marks']
    rows = dataframe_to_rows(df)
    
    for r_idx, row in enumerate(rows, 1):
        for c_idx, value in enumerate(row, 1):
             active2.cell(row=r_idx, column=c_idx, value=value)
    
    sheet4 = wb.create_sheet('marks1',0)
    active3 = wb['marks1']
    rows1 = dataframe_to_rows(df)
    for r_idx, row in enumerate(rows1, 1):
        for c_idx, value in enumerate(row, 1):
             active3.cell(row=r_idx, column=c_idx, value=value)
    wb.save('new.xlsx')
    

    您可以参考static-image-export 以及 df.to_excel using openpyxl了解更多详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 2020-11-04
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 2023-01-24
      • 1970-01-01
      相关资源
      最近更新 更多