这里刚刚进入Python并遇到了一个让我失望的问题。我有以下Python代码摘录,用于从现有模板创建PPT幻灯片。布局和占位符是正确的,但我无法使用下面列出的数据(x,y_in和& y_out)运行它。非常感谢任何帮助。
x = [datetime.datetime(2017, 8, 4, 15, 5, tzinfo=<FixedOffset u'+00:00' datetime.timedelta(0)>), datetime.datetime(2017, 8, 4, 15, 10, tzinfo=<FixedOffset u'+00:00' datetime.timedelta(0)>), datetime.datetime(2017, 8, 4, 15, 15, tzinfo=<FixedOffset u'+00:00' datetime.timedelta(0)>), datetime.datetime(2017, 8, 4, 15, 20, tzinfo=<FixedOffset u'+00:00' datetime.timedelta(0)>)]
y_in = [780993, 538962, 730180, 1135936]
y_out = [5631489, 6774738, 6485944, 6611580]
prs = Presentation('Network_Utilization_template_master.pptx')
slide = prs.slides.add_slide(prs.slide_layouts[2])
placeholder = slide.placeholders[17]
chart_data = CategoryChartData()
chart_data.categories = x
chart_data.add_series(y_in)
chart_data.add_series(y_out)
graphic_frame = placeholder.insert_chart(XL_CHART_TYPE.LINE, chart_data)
chart = graphic_frame.chart
chart.has_legend = True
chart.legend.include_in_layout = True
chart.series[0-2].smooth = True
prs.save("Network_Utilization_" + today_s + ".pptx")
编译器吐出以下内容:
Traceback (most recent call last):
File "/Users/jemorey/Documents/pptx-2.py", line 81, in <module>
graphic_frame = placeholder.insert_chart(XL_CHART_TYPE.LINE, chart_data)
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/shapes/placeholder.py", line 291, in insert_chart
rId = self.part.add_chart_part(chart_type, chart_data)
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/parts/slide.py", line 174, in add_chart_part
chart_part = ChartPart.new(chart_type, chart_data, self.package)
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/parts/chart.py", line 29, in new
chart_blob = chart_data.xml_bytes(chart_type)
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/data.py", line 104, in xml_bytes
return self._xml(chart_type).encode('utf-8')
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/data.py", line 128, in _xml
return ChartXmlWriter(chart_type, self).xml
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/xmlwriter.py", line 803, in xml
'ser_xml': self._ser_xml,
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/xmlwriter.py", line 902, in _ser_xml
'tx_xml': xml_writer.tx_xml,
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/xmlwriter.py", line 191, in tx_xml
'series_name': self.name,
File "/Users/jemorey/Library/Python/2.7/lib/python/site-packages/pptx/chart/xmlwriter.py", line 121, in name
return escape(self._series.name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/sax/saxutils.py", line 32, in escape
data = data.replace("&", "&")
AttributeError: 'list' object has no attribute 'replace'
答案 0 :(得分:0)
David Zemens的评论非常正确。系列有一个名称,它显示为ChartData.add_series()
的第一个参数。该名称显示在该系列的线条颜色旁边的图例中,也显示为该系列数据的列标题。添加它可以让你进入下一步。
类似的东西:
chart_data.add_series('MB in', y_in)
chart_data.add_series('MB out', y_out)