Save chart as image (png/jpg) with XlxsWriter

馋奶兔 提交于 2021-01-29 12:55:43

问题


I am generating excel files with XlxsWriter that include some charts. I would like to save the resulting charts separately as images so I can attach them to an email (as a preview of the Excel file I'm sending). I'm able to create the Excel with charts, I just can't find any way to save the charts as an image in the XlxsWriter documentation.

I am doing this on a Mac, so unfortunately the solution using win32 doesn't help. excel2img looked promising, but also depends on win32.

Example chart generation is as follows (borrowed from the documentation). It generates a simple Excel file with a chart. I would like a png/jpg export of the chart from this Excel.

import xlsxwriter

workbook = xlsxwriter.Workbook('chart.xlsx')
worksheet = workbook.add_worksheet()

# Create a new Chart object.
chart = workbook.add_chart({'type': 'column'})

# Write some data to add to plot on the chart.
data = [
    [1, 2, 3, 4, 5],
    [2, 4, 6, 8, 10],
    [3, 6, 9, 12, 15],
]

worksheet.write_column('A1', data[0])
worksheet.write_column('B1', data[1])
worksheet.write_column('C1', data[2])

# Configure the chart. In simplest case we add one or more data series.
chart.add_series({'values': '=Sheet1!$A$1:$A$5'})
chart.add_series({'values': '=Sheet1!$B$1:$B$5'})
chart.add_series({'values': '=Sheet1!$C$1:$C$5'})

# Insert the chart into the worksheet.
worksheet.insert_chart('A7', chart)

# TODO How can I get the chart as a png/jpg

workbook.close()

来源:https://stackoverflow.com/questions/61138235/save-chart-as-image-png-jpg-with-xlxswriter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!