1
votes

Newb here just getting into Python and ran into an issue that's beating me down. I have the following excerpt of Python code to create a PPT slide from an existing template. The layout and placeholders are correct but I can't get it to run with my data listed below (x, y_in, & y_out). Any help is greatly appreciated.

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")

the compiler spits out the following:

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("&", "&amp;")
AttributeError: 'list' object has no attribute 'replace'
1
Curious if you get the same error using string, int or other non-datetime types for the x values. Can you try with something simple like x = [1,2,3,4]? - David Zemens
Thanks David....i tried with list x = [1,2,3,4] as you suggested and received the exact same traceback. - Jerrance
the add_series method appears to take as it's first argument a str object representing the series Name. you're only passing a list, and I think that's causing the error. python-pptx.readthedocs.io/en/latest/user/charts.html - David Zemens

1 Answers

0
votes

David Zemens is quite right in his comment. A series has a name, which appears as the first argument to ChartData.add_series(). The name appears in the legend next to the line color for that series and also appears as the column heading for the data for that series. Adding that in should get you to your next step.

Something like:

chart_data.add_series('MB in', y_in)
chart_data.add_series('MB out', y_out)