drakmail's solution to "Python3: Add a line to to a slide "

Example of adding line:<\/p>\n

from<\/span> pptx.enum.shapes<\/span> import<\/span> MSO_CONNECTOR<\/span>\nfrom<\/span> pptx<\/span> import<\/span> Presentation<\/span>\nfrom<\/span> pptx.util<\/span> import<\/span> Inches<\/span>\n\nprs<\/span> =<\/span> Presentation<\/span>()<\/span>\n\nslide_layout<\/span> =<\/span> prs<\/span>.<\/span>slide_layouts<\/span>[<\/span>0<\/span>]<\/span>\nslide<\/span> =<\/span> prs<\/span>.<\/span>slides<\/span>.<\/span>add_slide<\/span>(<\/span>slide_layout<\/span>)<\/span>\n\nshapes<\/span> =<\/span> slide<\/span>.<\/span>shapes<\/span>\n\nline<\/span> =<\/span> shapes<\/span>.<\/span>add_connector<\/span>(<\/span>MSO_CONNECTOR<\/span>.<\/span>STRAIGHT<\/span>,<\/span> Inches<\/span>(<\/span>4<\/span>),<\/span> Inches<\/span>(<\/span>2<\/span>),<\/span> Inches<\/span>(<\/span>1<\/span>),<\/span> Inches<\/span>(<\/span>7<\/span>))<\/span>\n\nprs<\/span>.<\/span>save<\/span>(<\/span>'example.pptx'<\/span>)<\/span>\n<\/pre><\/div>
Example of adding line: ``` from pptx.enum.shapes import MSO_CONNECTOR from pptx import Presentation from pptx.util import Inches from pptx.dml.color import RGBColor prs = Presentation() slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(slide_layout) shapes = slide.shapes line = shapes.add_connector(MSO_CONNECTOR.STRAIGHT, Inches(4), Inches(2), Inches(1), Inches(7)) )) line.line.fill.solid() line.line.fill.fore_color.rgb = RGBColor.from_string("FF0000") prs.save('example.pptx') ```') ``` PS. Updated example to show how to set color for the line
Example of adding line: ``` from pptx.enum.shapes import MSO_CONNECTOR from pptx import Presentation from pptx.util import Inches, Pt from pptx.dml.color import RGBColor prs = Presentation() slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(slide_layout) shapes = slide.shapes line = shapes.add_connector(MSO_CONNECTOR.STRAIGHT, Inches(4), Inches(2), Inches(1), Inches(7)) )) # set solid fill line.line.fill.solid() () # set red color line.line.fill.fore_color.rgb = RGBColor.from_string("FF0000") ") # set width to 10pt line.line.width = Pt(10) prs.save('example.pptx') ``` PS. Updated example to show how to set color for the line PPS. Updated example to show how to set line width
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) element.attrib.update(kwargs) parent.append(element) 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 line.line.fill.solid() # 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 pptx.dml.color import RGBColor prs = Presentation() slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(slide_layout) shapes = slide.shapes line = shapes.add_connector(MSO_CONNECTOR.STRAIGHT, Inches(4), Inches(2), Inches(1), Inches(7)) # set0 solid fill line.line.fill.solid() # set red color line.line.fill.fore_color.rgb = RGBColor.from_string("FF0000") # set width to 10pt1.0 prs.save('example.pptx') ``` PS. Updated example to show how to set color for the line line.line.width = Pt(10) prs.save('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

User: drakmail

Question: Python3: Add a line to to a slide

Back to question