scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.47k stars 530 forks source link

Feature Request: Add shapes to Slide Master #575

Open marcrsc opened 4 years ago

marcrsc commented 4 years ago

Hello!

Sometimes we may need to customize the Master Slide to provide a really custom presentation, changing the background, a client's logo, include some fixed text, etc. Although we can insert this background and logo slide by slide, it is inefficient, and not so elegant...

I could see some workarounds suggesting replacing images in the master slide from a previous model (I could not make it work, it is in another thread), but it still requires a previously structured base presentation. The ideal is to be able to freely create this slide master runtime, from a totally blank presentation (including a blank master).

Trying to insert a shape using the command below returns an error: "AttributeError: 'MasterShapes' object has no attribute 'add_picture'", informing that it is not implemented:

prs = Presentation()
slide=prs.slide_master
img = slide.shapes.add_picture(image1, left, top, width, height)

I think that technically it is possible, considering Powerpoint specs because using Delphi I can customize the master slide simply adding shapes. It has some wrapper components that encapsulate some Microsoft APIs and there are working commands like this:

PPT.SlideMaster.Shapes.AddPicture(image1, 1, 1, left, top, width, height);

But the idea is to do that using Python with python-pptx, because Delphi requires Powerpoint installed and running in the machine to work (the app actually open - who creates the presentation is Powerpoint itself, the API just controls it) - this makes python-pptx much more practical and powerful, especially when generating presentations in a server.

Is there some way to insert texts, pictures, and other shapes to Master Slide? If not, is it possible to implement it in a near feature?

Thanks and regards!

scanny commented 4 years ago

There is no technical reason that a slide-master object couldn't have an .add_picture() method. Plenty of PowerPoints have a logo on the slide-master, for example. The question is who wants it bad enough to sponsor it. I occasionally add features to python-pptx because I need them for a project I'm working on. Other than that, features are added by sponsorships. We've tried accepting contributed features, but basically that doesn't work for this project because the engineering standards are high and contributors, who are usually not professional developers, in general can't meet them.

If you have project budget and want to consider a sponsorship for the features you need, let me know. That's how much of the library got there.

marcrsc commented 4 years ago

Hello Steve,

 Thank you for your reply!

  First, congratulations and thank you very much for your work on python-pptx – great work! Makes things really very easy to use, and the possibility of generating dynamic presentations even without Powerpoint installed makes it very powerful!

I totally understand your considerations about the nature of the work, and the need for some return if developing more specific features. I’m sure that it already contains long journeys of hard work and knowledge, in benefit of a whole community!

About the feature, there is the workaround of customizing the whole base presentation for each client, instead providing the elements to be added to a blank slide-master, although it is a more manual process. As I am still exploring and learning to use the python-pptx resources, I’m trying to reproduce the tasks I used to do with Delphi, so I found this issue. 

Anyway, it would be nice to have full automated control of the presentation creation process, so I’d like to ask, how much would be a proper sponsorship for this feature? I think it should be enough for now to add only pictures and text boxes* to a totally blank slide-master (blank layout, with no placeholders).

*slide_master.shapes.add_picture() and slide_master.shapes.add_textbox()

Thanks and regards,

Marcelo Carvalho.

De: Steve Canny notifications@github.com Enviada em: segunda-feira, 2 de dezembro de 2019 16:03 Para: scanny/python-pptx python-pptx@noreply.github.com Cc: mrscarvalho mc@arquivo.com.br; Author author@noreply.github.com Assunto: Re: [scanny/python-pptx] Feature Request: Add shapes to Slide Master (#575)

There is no technical reason that a slide-master object couldn't have an .add_picture() method. Plenty of PowerPoints have a logo on the slide-master, for example. The question is who wants it bad enough to sponsor it. I occasionally add features to python-pptx because I need them for a project I'm working on. Other than that, features are added by sponsorships. We've tried accepting contributed features, but basically that doesn't work for this project because the engineering standards are high and contributors, who are usually not professional developers, in general can't meet them.

If you have project budget and want to consider a sponsorship for the features you need, let me know. That's how much of the library got there.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/scanny/python-pptx/issues/575?email_source=notifications&email_token=ANSD4QGWALWJVKLUHKZT46LQWVLVLA5CNFSM4JTIOS2KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFURGIQ#issuecomment-560534306 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ANSD4QFPE2W6JIUO64BSDUTQWVLVLANCNFSM4JTIOS2A . https://github.com/notifications/beacon/ANSD4QA4SBSXVW237DPABRTQWVLVLA5CNFSM4JTIOS2KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFURGIQ.gif

rymarczy commented 3 years ago

I haven't tested this extensively, but changing the the inheritance of the LayoutShapes class in shapetree.py from _BaseShapes to _BaseGroupShapes appears to allow this behavior.

innovation-plantation commented 3 years ago

Maybe the documentation should mention that we don't support modifying a master and saving it with the presentation. I'll use xslt or something to get what I want now that I found an issue that indicates that this sort of thing is not supported through pypptx.

MartinPacker commented 3 years ago

I have - in several places - used the XML support shipped with python-pptx to extend its capabilities. I think you should consider that approach. (XSLT if you're familiar with it is great. If not it's not. And you would still need to embed it in the zip file and fix up other things in the zip file.)

innovation-plantation commented 3 years ago

After playing around with powerpoint my approach will be to set up a set of slide masters in PPT, then instead of using the constructor to create a default empty presentation, I'll use the empty presentation with my set of masters and select the layouts I need from it. I just spent about 2 hours figuring out that I couldn't do things like center text, remove bullets, adjust the font size, etc. This will work better anyway because there are effects that powerpoint does (glow in particular) that I can have it put into the masters that no other programs seem to process correctly, and I understand pypptx just passes all that stuff through ignoring it so it remains in the slide presentation. I generate presentations with around 80 slides in each, using only about half a dozen backgrounds but otherwise the same layout, so the efficient approach is to have one master per background. Anyway it works great once understanding its capabilities and limitations.

http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail Virus-free. www.avg.com http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Sun, Jan 24, 2021 at 1:13 AM Martin Packer notifications@github.com wrote:

I have - in several places - used the XML support shipped with python-pptx to extend its capabilities. I think you should consider that approach. (XSLT if you're familiar with it is great. If not it's not. And you would still need to embed it in the zip file and fix up other things in the zip file.)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/scanny/python-pptx/issues/575#issuecomment-766327494, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGHWKWRNXYUP5PPBKVPUN63S3P32DANCNFSM4JTIOS2A .

realwatch commented 2 years ago

I tried to add this function. First of all, Steve's work is really good. My main work is to copy a few functions related to the add_picture function under _BaseGroupShapes to MasterShapes. Then since MasterShapes doesn't have a _grpSP member, I changed it to a _spTree member, which shows that they actually refer to the same object during my debugging. Next is the id problem of the image. The original code assigns a very large id to the newly added image (the maximum value of the used ID + 1), which prevents the file from being opened by powerpoint, so I wrote a new @proprety function _next_pic_shape_id in _BaseShapes class, wrote a new @proprety function _next_pic_shape_id in the CT_GroupShape class, these two functions copy the _next_shape_id and _next_shape_id in their respective classes, just modify the calling relationship and modify the conditions in the xpath function from "@id" to "//p:cNvPr/@id". Make the id of the image only calculate the shape that contains the image The modified code is currently working fine in my environment, using

prs = Presentation(input_file)
for slide_master in prs.slide_masters:
    slide_master.shapes.add_picture(pic, left, top, width, height )
prs.save(output)

You can add images in MasterSlide

innovation-plantation commented 2 years ago

Great! Can background images also be added to masters?

On Mon, Jun 20, 2022 at 12:04 AM realwatch @.***> wrote:

I tried to add this function. First of all, Steve's work is really good. My main work is to copy a few functions related to the add_picture function under _BaseGroupShapes to MasterShapes. Then since MasterShapes doesn't have a _grpSP member, I changed it to a _spTree member, which shows that they actually refer to the same object during my debugging. Next is the id problem of the image. The original code assigns a very large id to the newly added image (the maximum value of the used ID + 1), which prevents the file from being opened by powerpoint, so I wrote a new @proprety https://github.com/proprety function _next_pic_shape_id in _BaseShapes class, wrote a new @proprety https://github.com/proprety function _next_pic_shape_id in the CT_GroupShape class, these two functions copy the _next_shape_id and _next_shape_id in their respective classes, just modify the calling relationship and modify the conditions in the xpath function from @." to @.". Make the id of the image only calculate the shape that contains the image The modified code is currently working fine in my environment, using

prs = Presentation(input_file) for slide_master in prs.slide_masters: slide_master.shapes.add_picture(pic, left, top, width, height ) prs.save(output)

You can add images in MasterSlide

— Reply to this email directly, view it on GitHub https://github.com/scanny/python-pptx/issues/575#issuecomment-1160240666, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGHWKWRV5Z7CXSARHVYO77LVQA62JANCNFSM4JTIOS2A . You are receiving this because you commented.Message ID: @.***>