Home » excel » python/excel cell -> png

python/excel cell -> png

Posted by: admin May 14, 2020 Leave a comment


There is an excel document that needs weekly updating… Just a few cells that need to be updated, which is totally doable via: http://www.python-excel.org/

After these cells are updated, a graph is generated inside excel. Is it possible to export this graph into a .png via python (ie, copy cells A3-B7 and export into an image)?

Thoughts, ideas?


How to&Answers:

You could export an existing chart to PNG using COM extensions. You will have to make sure the chart has been already updated with the new data before exporting.

I found this discussion of the Charts object helpful:

You would up with something like this (untested code):

from win32com.client import Dispatch
xlsApp = Dispatch("Excel.Application") 
xlsWB = xlsApp.Workbooks.Open(r'C:\TEST\WorkbookWithAChart.xlsx') 
xlsSheet = xlsWB.Sheets("Sheet 1") 
mychart = XlsSheet.Charts(1)  #'1' is the index of the chart object in the wb

Helpful references:


This is going to be impossible without actually using Excel. You need Excel to draw the graph with the updated information.

If you can use Excel, you can write a macro which exports the chart to a PDF file. Something like this:

Sheets("Sheet1").ChartObjects("Chart Name").Chart.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:="C:\folder\filename.pdf", _
    Quality:=xlQualityStandard, _