当前位置:  首页>> 技术小册>> Python与办公-玩转PPT

也可以修改自动形状边框的某些属性,比如说颜色和大小。Shape对象的边框是一个LineFormat对象,它的color属性控制边框的颜色和亮度,width属性控制边框的宽度,也就是边框的大小。边框的亮度是由color.brightness控制的,赋值为一个浮点数就行,取值范围是-1~1,当为-1的时候因为太暗了所以会显示为黑色,当为1时因为太亮了会显示为白色,当为0的时候是正常的亮度,参考代码如下:

  1. from pptx import Presentation
  2. from pptx.dml.color import RGBColor
  3. from pptx.util import Inches,Pt
  4. ppt = Presentation()
  5. slide = ppt.slides.add_slide(ppt.slide_layouts[0])
  6. left = top = width = height = Inches(3)
  7. shape = slide.shapes.add_shape(5,left,top,width,height)
  8. line = shape.line
  9. print(type(line))
  10. # 边框颜色
  11. line.color.rgb = RGBColor.from_string("00FF0")
  12. # 边框颜色亮度
  13. line.color.brightness = 0
  14. # 边框大小
  15. line.width = Pt(5)
  16. ppt.save("./ppt_ files/test.pptx")