Using openpyxl to modify an existing excel chart's Data Source/Range

1,588 views
Skip to first unread message

Larry Weisberg

unread,
Oct 30, 2019, 6:26:11 AM10/30/19
to openpyxl-users
I have an excel workbook that I treat basically as a template, and every month my Python script appends a new row - for example row #61 - (with Date and several numeric columns) on Sheet2.  So the excel modified today with October's data in row 61, will be the input next month to add November's data to Row 62.

On Sheet1 there are 3 different charts, each with Data Sources/Ranges referring to Sheet2.  For example one chart currently has:
=Sheet2!$B$3:$F$60,Sheet2!$H$3:$H$60

Is there a way with openpyxl to refer to the charts (e.g., by name, "Chart1", "Chart2", etc.) and then programatically change the Data Source/Range to now be, e.g., 
=Sheet2!$B$3:$F$61,Sheet2!$H$3:$H$61

Thanks!
Larry 

Charlie Clark

unread,
Oct 30, 2019, 10:54:34 AM10/30/19
to openpyxl-users
> =Sheet2!$B$3:$F$*61*,Sheet2!$H$3:$H$*61*

Charts are available in the ._charts attribute of a worksheet. This
basically because we don't have a clear way of naming them: by title, by
range, or by arbitrary order as you suggest. You can edit chart objects
but note that something like a Data Range object doesn't exist in a
chart object so you'll need to look at the OOXML specification and the
source code in order to work out what to do.

Charlie

--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226
Reply all
Reply to author
Forward
0 new messages