scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.38k stars 514 forks source link

Setting Chart.chart_style doesn't seem to behave as documented #407

Open jmm opened 6 years ago

jmm commented 6 years ago

Hello! Thanks for the work on this library 🙌

With python-pptx@0.6.10 the behavior of chart_style doesn't seem to match the docs for me:

[...] Range is from 1 to 48. [...] The integer index corresponds to the style’s position in the chart style gallery in the PowerPoint UI.

I'm wondering if part of the issue is that chart styles have changed between PowerPoint versions? Screenshots from PowerPoint 2011 for Mac show a chart styles gallery with 48 items, but screenshots from Powerpoint 2016 for Windows show 14 items.

I'd also note that within a version it varies per chart type. Example from PowerPoint for Mac v15:

Chart Type # of styles
Stacked bar 11
Clustered column 14
Pie 12
Doughnut 10

But most importantly, setting it to a number within the range of styles displayed for a given type does not seem to apply that style.

I did an experiment and generated 48 slides with doughnut charts with the chart style set to each number in the range 1-48 and none of them correspond to the 10 chart styles I see in the PowerPoint UI. In other words, selecting each chart never shows one of the chart styles as selected in the PowerPoint UI. The style / color scheme of the charts do vary.

Example:

chart_data = ChartData()
chart_data.categories = ['Yes', 'No']
chart_data.add_series('Series 1', (42, 24))

for chart_style in range(1, 49):
    slide = presentation.slides.add_slide(presentation.slide_layouts[1])
    placeholder = slide.placeholders[13]
    chart = placeholder.insert_chart(XL_CHART_TYPE.DOUGHNUT, chart_data).chart
    chart.chart_style = chart_style

So it seems like the docs are not accurate for all versions of PowerPoint.

Thanks!

scanny commented 6 years ago

@jmm Section 21.2.3.46 ST_Style (Style) on page 3450 (3460th physical page) of the spec here: https://github.com/scanny/python-pptx/blob/master/spec/ISO-IEC-29500-1/ISO-IEC-29500-1.pdf describes this attribute as "an integer between 1 and 48", and goes on to discuss the behavior in more depth than usual. I can't speak to how that corresponds to PowerPoint UI behavior, at least in other PowerPoint versions. I've been on PowerPoint 2011 for Mac since about, well, 2011 :)

This feature is here for folks that might want to use it for some reason, assuming they know what number they want (somehow). If I wanted to use it, I'd make a chart look the way I wanted with the UI (gallery) and then examine the XML (perhaps with opc-diag) to determine which number to use in my code. (e.g. browse chart1.xml and look at the style element)

All that said, if you want to submit a PR on the docs to clarify, I'm happy to consider it and probably get it into the next release :)

jmm commented 5 years ago

Thank you! I appreciate the helpful reply. (And sorry for the delay saying that.)

I've been on PowerPoint 2011 for Mac since about, well, 2011 :)

I know what you mean, if I have an app that works well enough I'm loath to update.

This feature is here for folks that might want to use it for some reason, assuming they know what number they want (somehow).

😂 fair enough.

I've been trying hard to avoid digging into the XML, but I appreciate the suggestion and tips.

I'm not sure what change did it (I did update python-pptx in the meantime, among other things), but the issue I needed to use chart style to address before was somehow taken care of by some other change and I was able to stop setting chart style (thankfully, since it's shrouded in mystery and maybe inconsistency).

I'm not sure when I'd have a chance to make a PR -- if someone else is able to, 🙌 . I do think it'd be good to update the docs and hopefully you don't mind keeping the ticket open until someone has a chance.