如何修改python-pptx中MSO_Connector的线条颜色
修改python-pptx中Connector线条颜色为黑色的方法
要把MSO_CONNECTOR绘制的线条改成黑色,你需要操作Connector对象的line属性,通过设置纯色填充并指定RGB颜色来实现,具体代码如下:
from pptx import Presentation from pptx.util import Inches from pptx.enum.shapes import MSO_CONNECTOR from pptx.dml.color import RGBColor # 初始化PPT和幻灯片 prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[0]) # 创建直线 line1 = slide.shapes.add_connector(MSO_CONNECTOR.STRAIGHT, Inches(1), Inches(1), Inches(2), Inches(2)) # 修改线条颜色为黑色 line = line1.line # 设置线条为纯色填充 line.fill.solid() # 指定RGB黑色 line.fill.fore_color.rgb = RGBColor(0, 0, 0) # 可选:调整线条宽度(如果需要) # from pptx.util import Pt # line.width = Pt(2) prs.save("black_line.pptx")
说明:
- 默认线条使用的是PPT主题的强调色,所以需要先通过
line.fill.solid()切换为纯色填充模式 - 使用
RGBColor(0, 0, 0)直接指定黑色,也可以用主题色索引(比如MSO_THEME_COLOR_INDEX.BACKGROUND1),但RGB方式更直接可控
内容的提问来源于stack exchange,提问作者Mary Sharyn
相关产品推荐
相关产品推荐

