使用XL_CHART_TYPE.LINE_MARKERS时如何设置标记颜色(线条和标记主体)?
chart.plots[0].series[0].marker.fill.fore_color = pptx.dml.color.RGBColor(0,0,0)
之类的技巧不起作用
下面的代码示例:
import traceback
import sys
from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Inches
import pptx
from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR,MSO_VERTICAL_ANCHOR, MSO_AUTO_SIZE,PP_ALIGN
from pptx.chart.data import CategoryChartData, ChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.enum.chart import XL_TICK_MARK
from pptx.enum.chart import XL_LEGEND_POSITION
from pptx.dml.chtfmt import ChartFormat
from pptx.dml.line import LineFormat
from pptx.dml.color import ColorFormat, RGBColor
from pptx.enum.dml import MSO_LINE
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
chart_data = ChartData()
chart_data.categories = ['Q1 Sales', 'Q2 Sales', 'Q3 Sales']
chart_data.add_series('West', (32.2, 28.4, 34.7))
chart_data.add_series('East', (24.3, 30.6, 20.2))
chart_data.add_series('Midwest', (20.4, 18.3, 26.2))
x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.LINE_MARKERS, x, y, cx, cy, chart_data
).chart
chart.has_legend = True
chart.legend.include_in_layout = False
chart.series[0].smooth = True
chart.plots[0].series[0].marker.style = 8
try:
fill = chart.plots[0].series[0].marker.format.fill
fill.solid()
fill.fore_color = RGBColor(0,0,0)
#chart.plots[0].series[0].marker.format.line.color = RGBColor(255,0,0)
#chart.plots[0].series[1].marker.style = 2
except Exception as error:
traceback.print_exc()
prs.save('chart-01.pptx')
它产生错误:
回溯(最近通话最近):文件 “”,第
38行 fill.fore_color = RGBColor(0,0,0)AttributeError:无法设置属性
答案 0 :(得分:0)
尝试:
.format
(请注意标记和填充之间的fill = series.points[0].format.fill
fill.solid()
fill.fore_color.rgb = RGBColor(255, 0, 0)
。)
这将为系列的所有标记设置颜色。
如果要设置各个点的颜色,则可以使用以下方法:
fill.solid()
.fore_color
调用将填充类型设置为实体,这在分配给.fore_color
之前是必需的,因为并非所有填充类型都具有{{1}}属性。