0
def new_presentation():
    prs=Presentation()
    img="C:/Users/Dennis/Desktop/Tom-Hiddleston-4-1024x768.jpg"
    mainsld=new_slide(prs, 6)
    mainshp=mainsld.shapes
    mainshp.add_picture(img, 0,0, Inches(10))
    titleshape=mainshp.add_shape(MSO_SHAPE.RECTANGLE, Inches(0), Inches(1), Inches(10), Inches(1))
    titleshape.fill.solid()
    titleshape.fill.fore_color.rgb=RGBColor(0x00,0x64,0x00)
    titleshape.fill.transparency = 0.25 ##doesnt work???##########################
    titleshape.line.fill.background()
    titlebox=mainshp.add_textbox(Inches(1), Inches(0.8),Inches(1), Inches(1)).text_frame.add_paragraph()
    titlebox.text="TOM HIDDLESTON"
    titlebox.font.name="Calibri"
    titlebox.font.size=Pt(36)
    titlebox.font.color.rgb=RGBColor(0x90,0x90,0x00)
    prs.save('test.pptx')

The line marked with "######s" is supposed to make the shape more transparent as written in the pptx documentation - it is a shape.fill property . Everything else in the code works perfectly. I'm using python 2.7 and latest pptx. Thank you for your help in advance.

Christian K.
  • 2,785
  • 18
  • 40

2 Answers2

5

After much digging, I have been able to come up with a solution for this

from pptx import Presentation
from pptx.oxml.xmlchemy import OxmlElement
from pptx.util import Cm
from pptx.enum.shapes import MSO_SHAPE
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(alpha))

## Create presentation
prs = Presentation()
## Add a slide (empty slide layout)
slide = prs.slides.add_slide(prs.slide_layouts[6])
##Add a blue box to the slide
blueBox = slide.shapes.add_shape(autoshape_type_id=MSO_SHAPE.RECTANGLE,
                         left=Cm(0),
                         top=Cm(0),
                         height=Cm(10),
                         width=Cm(20))
## Make the box blue
blueBoxFill = blueBox.fill
blueBoxFill.solid()
blueBoxFillColour = blueBoxFill.fore_color
blueBoxFillColour.rgb = RGBColor(0,176,240)
## Set the transparency of the blue box to 56%
_set_shape_transparency(blueBox,44000)
## Save the presentation
prs.save(your_path)
OD1995
  • 1,647
  • 4
  • 22
  • 52
  • how did you determine that the value corresponding to 56% transparency is 44000? Is there a general rule? Thanks – mrk May 31 '20 at 09:57
  • 1
    I believe 1000 is 99% transparency, 5000 is 95% transparency etc. More generally the formula would be `(100-t)*1000` where `t` is the level of transparency you want in % – OD1995 May 31 '20 at 10:13
1

FillFormat.transparency is not implemented yet. The part of the documentation where you saw that may have been an analysis page, which is a precursor to development.

This is the analysis page: http://python-pptx.readthedocs.io/en/latest/dev/analysis/features/dml-fill.html?highlight=transparency

This is the FillFormat (.fill) API, as developed: http://python-pptx.readthedocs.io/en/latest/api/dml.html#fillformat-objects

You can however use lxml calls from the FillFormat object to manipulate the XML under it. You probably want to start with the spPr element in the .fill element:

spPr = titleshape.fill._xPr
print spPr.xml

Here's one example of doing that sort of thing: https://groups.google.com/forum/#!msg/python-pptx/UTkdemIZICw/qeUJEyKEAQAJ

You'll find more if you search on various combinations of the terms python-pptx, OxmlElement, lxml, and workaround function.

scanny
  • 26,423
  • 5
  • 54
  • 80