问题
Is it possible to set/edit a Pie chart's border width in python-pptx?
I'd image something like this would do the trick?
for idx, point in enumerate(chart.series[0].points):
point.format.width = Pt(7.25)
回答1:
This feature is not yet supported by the python-pptx
API, but you can accomplish it by adapting a workaround like this:
from pptx.dml.color import RGBColor
from pptx.dml.line import LineFormat
from pptx.oxml import parse_xml
from pptx.oxml.ns import nsdecls
from pptx.util import Pt
plotArea = chart._chartSpace.plotArea
# ---get-or-add spPr---
spPrs = plotArea.xpath("c:spPr")
if len(spPrs) > 0:
spPr = spPrs[0]
else:
# ---add spPr---
spPr_xml = (
"<c:spPr %s %s>\n"
" <a:noFill/>\n"
" <a:ln>\n"
" <a:solidFill>\n"
" <a:srgbClr val=\"DEDEDE\"/>\n"
" </a:solidFill>\n"
" </a:ln>\n"
" <a:effectLst/>\n"
"</c:spPr>\n" % (nsdecls("c"), nsdecls("a"))
)
spPr = parse_xml(spPr_xml)
plotArea.insert_element_before(spPr, "c:extLst")
line = LineFormat(spPr)
line.color.rgb = RGBColor.from_text("DEDEDE")
line.width = Pt(2)
The LineFormat
object formed in this way has all the methods and properties described here:
https://python-pptx.readthedocs.io/en/latest/api/dml.html#lineformat-objects
It probably makes sense to extract most of that code into a method chart_border(chart)
that returns the LineFormat
object for the chart, after which you can manipulate it in the usual way.
来源:https://stackoverflow.com/questions/54985891/python-pptx-change-border-width-for-pie-chart