2

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'
vagabond
  • 3,526
  • 5
  • 43
  • 76
  • 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 Nov 28 '16 at 20:23
  • Yes , I read it in the documentation and thought it's implemented already . May have misunderstood. I'll share the link . – vagabond Nov 28 '16 at 20:24
  • Here's the link : http://python-pptx.readthedocs.io/en/latest/dev/analysis/cht-title.html?highlight=title . 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 Nov 28 '16 at 20:32
  • 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 Nov 28 '16 at 20:36

1 Answers1

3

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.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • So is there a way to add the slide_title instead? I was trying : `title_placeholder = slide.shapes.title title_placeholder.text = sheetname` but again get error : AttributeError: 'NoneType' object has no attribute 'text' . – vagabond Nov 28 '16 at 20:45
  • @vagabond `slide.shapes.title.text = sheetname` is the simplest. The API docs can help you if you want to dig in and do things like change the formatting of an individual word in the title or something. – scanny Nov 28 '16 at 20:46
  • same error : `---> 25 slide.shapes.title.text = sheetname 26 27 prs.save('scatter_charts.pptx') AttributeError: 'NoneType' object has no attribute 'text'` – vagabond Nov 28 '16 at 20:47
  • I don't have a placeholder called 'title' . When I run: `for shape in slide.placeholders: ... print('%d %s' % (shape.placeholder_format.idx, shape.name))` - I get : `11 Text Placeholder 3 12 Text Placeholder 4 13 Text Placeholder 2 15 Text Placeholder 1` . So I'm trying to figure out how to add text into the placeholder. – vagabond Nov 28 '16 at 20:48
  • @vagabond we're drifting into a separate question. Why don't you post it separately. If you tag it with 'python-pptx' I'll see it. – scanny Nov 28 '16 at 20:50
  • Yep - agreed . I created a new question . http://stackoverflow.com/questions/40853330/adding-text-slide-title-to-placeholder-on-slide-with-python-pptx – vagabond Nov 28 '16 at 20:58