Python Insert Image into the middle of an existing PowerPoint - python

I have an existing PowerPoint presentation with 20 slides. This presentation serves as an template with each slide having different backgrounds. I want to take this (existing) PowerPoint presentation, insert an image in slide number 4 (do nothing with the first 3) and save it as a new PowerPoint presentation.
This is what I have up until now. This code loads an existing presentation and saves it as a new one. Now I just need to know how to use this to insert an image to slide number 4 like described above.
Note: I am using normal Python.
from pptx import Presentation
def open_PowerPoint_Presentation(oldFileName, newFileName):
prs = Presentation(oldFileName)
#Here I guess I need to type something to complete the task.
prs.save(newFileName)
open_PowerPoint_Presentation('Template.pptx', 'NewTemplate.pptx')

I'm not really familiar with this module, but I looked at their quickstart
from pptx.util import Inches
from pptx import Presentation
def open_PowerPoint_Presentation(oldFileName, newFileName, img, left, top):
prs = Presentation(oldFileName)
slide = prs.slides[3]
pic = slide.shapes.add_picture(img, left, top)
prs.save(newFileName)
open_PowerPoint_Presentation('Template.pptx', 'NewTemplate.pptx',
'mypic.png', Inches(1), Inches(1))

Related

change all fonts in powerpoint without opening the file

I wanted change the all fonts in about 100 powerpoint files, without opening the files. There are several shape types in each slide and each might have a different font. I used python-pptx package and wrote the following code to change the fonts of all texts in a powerpoint presentation. Although it does not give any error, it does not work, and the fonts in the file are still whatever they were, for example Arial. I also added print(shape.text) to make sure that it has found all texts, and it seems that there is no issue there. Is it a bug? Or am I missing anything?
prs = Presentation('f10.pptx')
for i, slide in enumerate(prs.slides):
for shape in slide.shapes:
print (shape.has_text_frame)
if shape.has_text_frame:
print(shape.text)
for p in shape.text_frame.paragraphs:
for r in p.runs:
print(r.font.name)
r.font.name = 'Tahoma'
print(r.font.name)
prs.save('f10_tahoma.pptx')
Besides, it seems that the package does not work for utf-8 characters. I added a text-box on the last slide by adding:
text_frame = shape.text_frame
text_frame.clear() # not necessary for newly-created shape
p = text_frame.paragraphs[0]
run = p.add_run()
run.text = 'سلام '
font = run.font
font.name = 'Andalus'
font.size = Pt(18)
before saving the file to add a textbox with utf-8 characters. It adds it there, and when I check the font it shows that it is set to Andalus, but actually it is not Andalus.
With Aspose.Slides for Python via .NET, you can easily change all fonts for all texts in your presentations. The following code example shows you how to do this:
import aspose.slides as slides
with slides.Presentation('example.pptx') as presentation:
for slide in presentation.slides:
for shape in slide.shapes:
if isinstance(shape, slides.AutoShape):
for paragraph in shape.text_frame.paragraphs:
for portion in paragraph.portions:
portion.portion_format.latin_font = slides.FontData('Tahoma')
You can also evaluate Aspose.Slides Cloud SDK for Python for presentation manipulating. This REST-based API allows you to make 150 free API calls per month for API learning and presentation processing.
Aspose Slides Online Viewer can be used to view presentations without PowerPoint installed.
I work at Aspose.
What language is the text of the file? Run.font properties work fine for UTF-8, but there is a separate font for cursive scripts like Arabic. Access to that secondary font is not implemented in python-pptx unfortunately, but that could explain at least part of the behavior you're seeing.
For roman character text (like that we're using here), there are a couple things to check.
The font in question needs to be installed on the machine PowerPoint is running on when the document is opened. Otherwise PowerPoint will substitute a font.
The font (typeface) name used in the XML will not always exactly match what appears in the PowerPoint drop-down selection box. You need to give that name to python-pptx in the exact form it should appear in the XML. You may need to make an example file that works by hand, perhaps containing a single slide with a single textbox for simplicity, and then inspect the XML of that file to find the "spelling" used for that typeface by PowerPoint.
You could do that with code like this:
prs = Presentation("example.pptx")
shape = prs.slides[0].shapes[0]
print(shape._element.xml)
You should be able to locate the typeface name somewhere in an element like <p:rPr> or <p:defRPr>.

Extract presenter notes from PPTx file (powerpoint)

Is there a solution in python or php that will allow me to get the presenter notes from each slide in a power point file?
Thank you
You can use python-pptx.
pip install python-pptx
You can do the following to extract presenter notes:
import collections
import collections.abc
from pptx import Presentation
file = 'path/to/presentation.pptx'
ppt=Presentation(file)
notes = []
for page, slide in enumerate(ppt.slides):
# this is the notes that doesn't appear on the ppt slide,
# but really the 'presenter' note.
textNote = slide.notes_slide.notes_text_frame.text
notes.append((page,textNote))
print(notes)
The notes list will contain all notes on different pages.
If you want to extract text content on a slide, you need to do this:
for page, slide in enumerate(ppt.slides):
temp = []
for shape in slide.shapes:
# this will extract all text in text boxes on the slide.
if shape.has_text_frame and shape.text.strip():
temp.append(shape.text)
notes.append((page,temp))

Adding video in ppt using python cannot be played

I have to insert a video in ppt using python-pptx library. And also i have added the below code to insert it:
from pptx import Presentation
from pptx.util import Inches
prs = Presentation('template.pptx')
filepath = "file2.pptx"
layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(layout9)
path = 'video.mp4'
movie=slide.shapes.add_movie(path
, Inches(4.51), Inches(1.53), Inches(6.98),
Inches(4.69),poster_frame_image=None,mime_type='video/unknown'
)
prs.save(filepath)
This code successfully creates video shape. It show a big speaker icon when i click it for preview, it doesn't play at all . I dont know what i missed here. If anyone could help me please give some suggestion for this.
Aspose.Slides for Python makes it easy to add video to a presentation slide. The following code example shows you how to do this:
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
# Add a video frame to the slide.
video_frame = slide.shapes.add_video_frame(20, 20, 400, 300, "video.mp4")
# Add a poster image to presentation resources.
with open("poster.png", "rb") as poster_stream:
poster_image = presentation.images.add_image(poster_stream)
# Set the poster for the video.
video_frame.picture_format.picture.image = poster_image
presentation.save("example.pptx", slides.export.SaveFormat.PPTX)
This a paid product, but you can get a temporary license to evaluate all features of this library. I work as a Support Developer at Aspose.

Copy and paste image from a ppt file to another ppt file using python

I am using the below code to copy an image from one ppt to another ppt.but it is not working
ppt = win32com.client.Dispatch('Powerpoint.Application')
ppt1 = ppt.Presentations.Open("path\\temp.pptx")
ppt2 = ppt.Presentations.Open("path\\2paste.pptx")
ppt1.slide.Shapes.Copy()
ppt2.slide.Shapes.Paste()
This code opens both the ppt but it is not copying the images.
With Aspose.Slides for Python, you can easily copy any shapes from a presentation to another. The following code example shows you how to do this:
import aspose.slides as slides
file_name1 = "temp.pptx"
file_name2 = "2paste.pptx"
with slides.Presentation(file_name1) as presentation1:
with slides.Presentation(file_name2) as presentation2:
# Get the first shape from the first slide, for example.
first_shape = presentation1.slides[0].shapes[0]
# Clone the shape and add the clone to the second presentation.
presentation2.slides[0].shapes.add_clone(first_shape)
# Save the second presentation.
presentation2.save(file_name2, slides.export.SaveFormat.PPTX)
This is a paid library, but you can get a temporary licence for evaluating all features for your purposes. I work as a Support Developer at Aspose.

Python PPTX: Adding Entire Slide from Another Presentation

It seems this issue was brought up and requested back in 2015 but I cannot find any updates on it. I'm trying to copy an entire slide (including its text and images) from another presentation into my working presentation by doing something like the following:
prs = Presentation('CurrentPresentation.pptx')
prs1 = Presentation('OtherPresentation.pptx')
# Wanted_Slide = prs1.slides[0]
New_Slide = prs.slides.add_slide(prs1.slide_layout[0])
But all this does is add a totally blank slide (with the wanted slide's background) with slide layout 0, which totally makes sense. I know that's not the right way to do it. I tried the below and it did add a slide, but it was just a duplicate one of what was already in the prs presentation (I guess I found a way to duplicate a slide already in the presentation inadvertently):
def Add_Slide(self):
xml_slides = prs.slides._sldIdLst
xml_slides1 = prs1.slides._sldIdLst
slides = list(xml_slides)
slides1 = list(xml_slides1)
xml_slides.append(slides1[0])
The above code is a manipulation of a slide delete method I found online.
Or does anyone have any sort of recommendation on how to completely copy a slide and all of its contents over to a working presentation?
I apologize if no developments have been made and this post is a rehash. Thank you in advance.
There's an issue in the library's repo that has some code to do this, but it's rough, it doesn't work for all cases.
from pptx.parts.chart import ChartPart
from pptx.parts.embeddedpackage import EmbeddedXlsxPart
from pptx import Presentation
import copy
def _get_blank_slide_layout(pres):
layout_items_count = [len(layout.placeholders) for layout in pres.slide_layouts]
min_items = min(layout_items_count)
blank_layout_id = layout_items_count.index(min_items)
return pres.slide_layouts[blank_layout_id]
def move_slide(pres1, pres, index):
"""Duplicate the slide with the given index in pres1 and "moves" it into pres.
Adds slide to the end of the presentation"""
source = pres1.slides[index]
blank_slide_layout = _get_blank_slide_layout(pres)
dest = pres.slides.add_slide(blank_slide_layout)
for shape in source.shapes:
newel = copy.deepcopy(shape.element)
dest.shapes._spTree.insert_element_before(newel, 'p:extLst')
for key, value in source.part.rels.items():
# Make sure we don't copy a notesSlide relation as that won't exist
if "notesSlide" not in value.reltype:
target = value._target
# if the relationship was a chart, we need to duplicate the embedded chart part and xlsx
if "chart" in value.reltype:
partname = target.package.next_partname(
ChartPart.partname_template)
xlsx_blob = target.chart_workbook.xlsx_part.blob
target = ChartPart(partname, target.content_type,
copy.deepcopy(target._element), package=target.package)
target.chart_workbook.xlsx_part = EmbeddedXlsxPart.new(
xlsx_blob, target.package)
dest.part.rels.add_relationship(value.reltype, target,value.rId)
This function is an alteration of what is on the github comments at the location cited above. It works well for me, haven't tested it with charts or anything. It's slightly altered from what was posted on the site, because there was the rough example noted by the original answer, and there was a more thorough answer for duplicating within a presentation. Figured I'd post since it took me a while to get this all put together.
This solution has mainly worked with one exception. After copying the slide over to the destination presentation when I open the destination presentation I get the following warning:
PowerPoint couldn't read some content in xxx.pptx and removed it. Please check your presentation to see if the rest of it looks ok.
The rest of the presentation does look fine and after saving and reopening the warning goes away. Do you know of a way to make it so this warning doesn't appear or if there is a way to resolve it with Python (saving/closing again?).

Categories

Resources