首页 > 解决方案 > python pptx 如何根据 XL_CHART_TYPE.LINE 更改图表中的线条格式

问题描述

如何根据 XL_CHART_TYPE.LINE 访问图表中系列的线格式对象?

chart_data = ChartData()
vec2 = [ float(i.value) for i in xl_sheet.col(1)]
chart_data.categories = [ datetime.datetime.fromordinal(int(693594+i.value )) for i in xl_sheet.col(0)]
uu=chart_data.add_series('Model1',    tuple(vec2))

我没有访问系列行的格式

uu.format.line.color.rgb = RGBColor((100,100,100)) 

标签: pythonpython-pptx

解决方案


创建图表后,您可以通过线代表的系列访问每条线:

graphic_frame = slide.shapes.add_chart(..chart_data,..)
# ---graphic frame is the shape containing the chart, not the chart itself---
chart = graphic_frame.chart
# ---get the desired series from the chart---
series = chart.series[0]
# ---series.format is a ChartFormat object, which in turn provides access to
#    the LineFormat object for that series---
line = series.format.line
# ---change the line style, color, etc. the same way as for any other line---
line.color.rgb = RGBColor(255, 0, 0)

推荐阅读