I am trying to add a title to my powerpoint chart created using python-pptx library. I am on OSX with Python 2.7 and Python pptx version 0.6.1.
The code to create the charts is below :
df = pd.read_excel("/Users/vagabond/Documents/python_pptx_3/Bar_Charts.xlsx", sheetname=None)
f = open('/Users/vagabond/Documents/python_pptx_3/Presentation1.pptx')
prs = ppt.Presentation(f)
for sheetname, data in df.iteritems():
slide_layout = prs.slide_layouts[9]
slide = prs.slides.add_slide(slide_layout)
chart_data = ppt.chart.data.XyChartData()
series_1 = chart_data.add_series('Series 1')
## iterate through rows of the data frame for desired columns and add data points
for index, row in data.iterrows():
series_1.add_data_point(row['Share_of_apples'], row['Share_of_oranges'])
## define co-ordinates on the slide
x, y, cx, cy = Inches(1), Inches(3), Inches(7), Inches(4)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.XY_SCATTER, x, y, cx, cy, chart_data
).chart
prs.save('scatter_charts.pptx')
Now to add a title, I added the following after the slide.shapes.add_chart call:
chart.has_title = True
chart.title = sheetname
In case you are wondering, the value sheetname in chart.title = sheetname are the dict keys of dict df which I read in the first line. For every chart , I want the key of the key-value pair in the dictionary to be assigned. The program runs without any error.
The problem is , it does not add any chart titles.
I checked if chart.chart_title contains any value and it gives me an error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-26-7cb6c1e79da2> in <module>()
1 chart.has_title = True
2
----> 3 chart.chart_title.has_text_frame = True
AttributeError: 'Chart' object has no attribute 'chart_title'
python-pptxhadChart.chart_titlein its API? Sometimes folks find one of the enhancement proposals on search and take that as "as built" information where it's actually "wouldn't it be cool if it did this?" information :) - scanny