问题
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