Export chart as image with python xlwings

2.3k views Asked by At

I have been working on creating Excel reports automatically using Python and xlwings. Within these reports there are multiple charts and graphs which I now want to use in a pptx presentation keeping the same formatting and design. For the generation of the pptx I am currently using python-pptx.

An ideal solution would be to export the graphs directly from Excel to Powerpoint using xlwings, python-pptx, or some other library, maintaining the format and design of the graphs and keeping the dataset editable within the presentation.

A second solution would be to export the graphs while they are being created in the Excel report to a temporary folder as images. Then, use python-pptx to upload these images to the presentation. These would ensure keeping the design of the graphs but would mean that the data may not be edited. For this, I would need to know how to save charts as images with xlwings which I can't find a way to do so.

I am open to different solutions, including scraping over the generated Excel with a different language (a priori and without investigation, I would guess C# could be pretty good with windows apps). Although this would mean increasing the understanbility of the project as a whole.

Thanks very much in advance

2

There are 2 answers

1
Felix Zumstein On BEST ANSWER

You can implement a solution based on xlwings by falling back to the underlying pywin32 object and by adopting the solution from VBA, see e.g.: https://stackoverflow.com/a/11939249/918626

To learn about how to work on the underlying pywin32 object, see: http://docs.xlwings.org/en/stable/missing_features.html

0
Maxwell Miller On

Older question but I have a suggestion for solution 2, saving images using xlwings

This is slightly modified from another post xlwings: copy range and save as an image

import xlwings as xw

sht.range("Print_Area").api.CopyPicture(Appearance=1, Format=2)
sht.api.Paste()
sht.pictures[0].api.CopyPicture

The part I changed was to use Appearance 1 and Format 2 as options in the CopyPicture line, that allowed me to work with the result a little better than what was in the OG post. From there you can use your clipboard for static images in your PPTX