问题
I have created ppt using the below code:
prs = Presentation()
class MySlide:
def __init__(self, data):
self.layout = prs.slide_layouts[data[2]]
self.slide=prs.slides.add_slide(self.layout)
self.title=self.slide.shapes.title
self.title.text=data[0]
self.subtitle=self.slide.placeholders[1]
self.subtitle.text=data[1]
slides = [
["USA Weather", #data[0]
"Subtitle(Bullet)",
3],
["Malaysia Weather", #data[0]
"Content(Bullet)",
3],
["China Weather", #data[0]
"This is a brown Fox",
3]
]
for each_slide in slides:
MySlide(each_slide)
Now I am trying to add dataframe/image and descriptive text to above slides. I am not sure I understand how I am supposed to do it.
Previously I had tried the below code:
list_of_datasets = [df1, df2, df3]
list_of_slides = [slide[0], slide[1], slide[2]]
for i, j in zip(list_of_datasets, list_of_slides):
df_to_table(j, i)
But the above code puts table in complete slide which is not what I need. if someone can point to relevant documentation that should also be good for me since I did not understand the existing documentation regarding the same on: https://python-pptx.readthedocs.io/en/latest/dev/analysis/sld-layout.html
How to write data in different slide layouts using python-pptx?
回答1:
Put an image called for ex. "girl.png" in the directory to see this code working
from pptx import Presentation
import os
prs = Presentation()
class MySlide:
def __init__(self, data):
self.layout = prs.slide_layouts[data[3]]
self.slide=prs.slides.add_slide(self.layout)
self.title=self.slide.shapes.title
self.title.text=data[0]
self.subtitle=self.slide.placeholders[1]
self.subtitle.text=data[1]
if data[2] != "":
self.slide.placeholders[2].insert_picture(data[2])
slides = [
["USA Weather", #data[0]
"Subtitle(Bullet)",
"girl.png",
3],
["Malaysia Weather", #data[0]
"Content(Bullet)",
"",
3],
["China Weather", #data[0]
"This is a brown Fox",
"",
3]
]
for each_slide in slides:
MySlide(each_slide)
prs.save("stack.pptx")
os.startfile("stack.pptx")
来源:https://stackoverflow.com/questions/59496023/adding-dataframe-and-text-in-ppt-using-python-pptx-in-slide-layout-3