python-pptx: change border width for pie chart

情到浓时终转凉″ 提交于 2020-08-26 10:07:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!