简体   繁体   English

使用 python-pptx 的图表的图表标题

[英]Chart Title for Chart using python-pptx

I am trying to add a title to my powerpoint chart created using python-pptx library.我正在尝试为使用python-pptx库创建的 powerpoint 图表添加标题。 I am on OSX with Python 2.7 and Python pptx version 0.6.1.我在 OSX 上使用 Python 2.7 和 Python pptx 版本 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:现在要添加标题,我在slide.shapes.add_chart调用之后添加了以下内容:

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.如果您想知道, chart.title = sheetname中的值 sheetname 是我在第一行中读取的 dict df 的字典键。 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:我检查了 chart.chart_title 是否包含任何值,它给了我一个错误:

---------------------------------------------------------------------------
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'

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更新Chart.chart_titleChart.has_title已添加到python-pptx因为这个问题的原始答案: https : Chart.chart_title 。 chart.Chart.chart_title


python-pptx has no API support for chart titles yet. python-pptx还没有对图表标题的 API 支持。

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).您对Chart.has_title的分配没有给出错误的原因是因为 Python 在第一次分配时添加了该属性(因为它是一种动态语言)。 That attribute is not there in the API. API 中没有该属性。

以下代码行将起作用:

chart.chart.chart_title.text_frame.clear

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM