1
votes

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'
1
Was there something you read that led you to believe python-pptx had Chart.chart_title in 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
Yes , I read it in the documentation and thought it's implemented already . May have misunderstood. I'll share the link . - vagabond
Here's the link : python-pptx.readthedocs.io/en/latest/dev/analysis/… . There's a line which says , "Although it will not yet be supported, the chart title can be specified in the XML as a cell reference in the Excel worksheet. In general, any constructive operations on the title will remove this." I didn't know how to interpret it. - vagabond
Yes, that one is an enhancement proposal. In fact I think there's a pull request on the repo for that feature but it seems to have stalled. The way you can tell an enhancement proposal is that it is in the dev/analysis subtree of the documentation; but maybe I should start putting that at the title of each of those pages along with the status. These pages are essentially developer documentation to understand how to implement it and what behaviors that part of the API should have. - scanny

1 Answers

2
votes

UPDATE: Chart.chart_title and Chart.has_title have been added to python-pptx since this original answer to the question: https://python-pptx.readthedocs.io/en/latest/api/chart.html#pptx.chart.chart.Chart.chart_title


python-pptx has no API support for chart titles yet.

The reason your assignment to Chart.has_title doesn't give an error is because Python adds that attribute on first assignment (because it's a dynamic language). That attribute is not there in the API.