Reputation: 1091
The following lines of codes shows how to draw a line in PowerPoint slides using python. However, the four parameters in inches listed below only takes positive values so could not draw backwards. Is there any other way to draw lines in PowerPoint slides?
from pptx.enum.shapes import MSO_SHAPE
line1 = slide.shapes.add_shape(MSO_SHAPE.LINE_INVERSE, Inches(6), Inches(6), Inches(1), Inches(2))
Upvotes: 4
Views: 5377
Reputation: 2919
You can draw lines using the connector object versus the shape object, (Shape requires x,y, height, and width and powerpoint can't deal with negative heights)
From the docs
Lines are a sub-category of auto shape that differ in certain properties and behaviors. In particular, they have a start point and end point in addition to extents (left, top, width, height).
Connectors are based on the element and have one of a handful of different preset geometry values, such as line. Freeform connectors, despite the name, are not connectors, and are a custom geometry shape based on the p:sp element.
Connectors can be “connected” to an auto shape such that a connected end point remains connected when the auto shape is moved. This relies on a concept of “connection points” on the auto shape. These connections points are preset features of the auto shape, similar to how adjustment points are pre-defined. Connection points are identified by index.
Using the code below allows you to draw a straight line from x = 4 inches to x= 2 inches.
from pptx.enum.shapes import MSO_CONNECTOR
from pptx import Presentation
# Make sure you have a presentation called test1.pptx in your working directory
prs = Presentation(pptx='test1.pptx')
slide = prs.slides.add_slide(prs.slide_layouts[1])
#shapes.add_connector(MSO_CONNECTOR.STRAIGHT, start_x, start_y, end_x, end_y
line1=slide.shapes.add_connector(MSO_CONNECTOR.STRAIGHT, Inches(4), Inches(2), Inches(1), Inches(2))
prs.save('test2.pptx')
Upvotes: 5