Python3: Add a line to to a slide
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Looking for a simple solution for adding a line to power point slide using the current pptx library.
Must be able to:

1) set color,

2) set line width,

3) set transparency,

4) set line end style,

5) set positions

Something like:

from pptx import Presentation

prs = Presentation("template_Fred.pptx")

line = shape.line

line.color.rgb = RGBColor(255, 30, 100).

line.left = Inches(1).

....# set other stuff....'Fred Clone.pptx')

what do you mean by line end style?
ST2-EV 1 year ago
Sorry, cap type: round, square, flat.
broadreach 1 year ago
awarded to drakmail

Crowdsource coding tasks.

1 Solution

Winning solution

Example of adding line:

from pptx.enum.shapes import MSO_CONNECTOR, MSO_SHAPE
from pptx.oxml.xmlchemy import OxmlElement
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor

def SubElement(parent, tagname, **kwargs):
  element = OxmlElement(tagname)
  return element

def _set_shape_transparency(shape, alpha):
  """ Set the transparency (alpha) of a shape"""
  ts = shape.fill._xPr.solidFill
  sF = ts.get_or_change_to_srgbClr()
  sE = SubElement(sF, 'a:alpha', val=str(int(alpha * 100000)))

prs = Presentation()

slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(slide_layout)

shapes = slide.shapes

# position: top, left, width, height
line = shapes.add_connector(MSO_CONNECTOR.STRAIGHT, Inches(4), Inches(2), Inches(1), Inches(7))
# set solid fill
# set red color
line.line.fill.fore_color.rgb = RGBColor.from_string("FF0000")
# set width to 10pt
line.line.width = Pt(10)
# set transparency
_set_shape_transparency(line.line, 0.35) # alpha can be any value starting from 0.0 to 1.0'example.pptx')

PS. Updated example to show how to set color for the line
PPS. Updated example to show how to set line width
PPS. Updated example to show how to set line transparency

Needs to demonstrate how to change the other attributes: color, thickness/width....
broadreach 1 year ago
Perfect, thanks!
broadreach 1 year ago
Perfect, thanks!
broadreach 1 year ago