How to save excel chart in picture (e.g. JPG or PNG) through Python

5,114 views
Skip to first unread message

Ning

unread,
Jun 16, 2009, 10:48:10 AM6/16/09
to python-excel
Hi all python experts,

I have used win32com to update the data in one excel sheet, which also
lead the update of the chart. However, my client want to see the
picture instead of excel sheet.

Is that anyway I can convert the chart to picture through python?

Ning

Thomas P. Boesen

unread,
Jun 16, 2009, 11:43:07 AM6/16/09
to python...@googlegroups.com
Hi,

If you open the file in Excel, you can save the charts as graphics
files. You simply use the Export function on the chart object.

Note that if your chart is embedded in a worksheet, the Chart object is
inside a ChartObject object, that can be found as part of the
Worksheet.ChartObjects collection.

The screenshot shows the necessary Python commands entered into the
console of my DiscoveryScript Excel add-in (free download from
www.xefion.com) which makes it easy to use Python inside Excel.

:-) Thomas

John Machin

unread,
Jun 16, 2009, 11:53:13 AM6/16/09
to python...@googlegroups.com

Hi Ning,

Will the client accept a PDF file instead of JPEG or PNG? If so, you
should be able to use win32com to print to a pseudo-printer that makes
PDF files.

HTH,

John

Thomas P. Boesen

unread,
Jun 16, 2009, 11:55:38 AM6/16/09
to python...@googlegroups.com
Somehow the screenshot was lost from the email. Have a look here,
instead: xefion.com/excel_export_chart.png
:-) Thomas

Chris Withers

unread,
Jun 16, 2009, 12:31:45 PM6/16/09
to python...@googlegroups.com
Ning wrote:
> Is that anyway I can convert the chart to picture through python?

When I've been here in the past, I've just switched to using Matplotlib
to generate the charts; much more powerful and configurable *and* cross
platform ;-)

Chris

--
Simplistix - Content Management, Zope & Python Consulting
- http://www.simplistix.co.uk

Ning

unread,
Jun 17, 2009, 4:23:13 AM6/17/09
to python-excel
Thanks all.
I have found the solution online. Basically, we can use Export
funciton. See the example below:

xlApp = Dispatch("Excel.Application")
ExcelWorkBook = xlApp.Workbooks.Open(r'M:\development\Important
\Excel_Templates\HelloWorld.xlsx')
xlSheet1 = xlApp.Sheets("DATA")
chart = xlApp.Charts('HelloWorld')
Fname = r'M:\Excel\HelloWorld.png'
chart.Export(Filename=Fname)

Weijie Wu

unread,
Jun 19, 2014, 5:24:11 AM6/19/14
to python...@googlegroups.com
but this code only works on windows, right ? how we can export chart through python on CentOS ?

在 2009年6月17日星期三UTC+8下午4时23分13秒,Ning写道:
Reply all
Reply to author
Forward
0 new messages