python pptx怎么复制ppt_复制带有图像的幻灯片python pptx

本文介绍了一种在Python中更改PowerPoint演示文稿主题的方法,并详细解释了如何将带有特定主题的幻灯片内容(包括文本、文本框及图片)从一个模板复制到另一个模板的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

My end goal is to change the theme of a presentation. To do this, I have created a source template and new template (with the correct theme). I iterate over each slide in the source template then add a new slide to the new template with the contents of the source using the code below - source. If there is a better way to do this I'd love to hear it.

This works great for text and text boxes, however the test image cannot be displayed in the new powerpoint (show in the image below):

Code

def copy_slide_from_external_prs(self, src, idx, newPrs):

# specify the slide you want to copy the contents from

src_slide = src.slides[idx]

# Define the layout you want to use from your generated pptx

slide_layout = newPrs.slide_layouts[2]

# create now slide, to copy contents to

curr_slide = newPrs.slides.add_slide(slide_layout)

# remove placeholders

for p in [s.element for s in curr_slide.shapes if 'Text Placeholder' in s.name or 'Title' in s.name]:

p.getparent().remove(p)

# now copy contents from external slide, but do not copy slide properties

# e.g. slide layouts, etc., because these would produce errors, as diplicate

# entries might be generated

for shp in src_slide.shapes:

el = shp.element

newel = copy.deepcopy(el)

curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')

return newPrs

I was trying many different solutions and tried creating a new Picture using the image.blob property in the source image. However, then the image does not have an element. Do I need to convert the blob to a PNG, save it, then create a new image using that saved PNG?

There must be a better way to do this. Again, I just want to change the theme.

Thanks in advance!

解决方案

Here is the workaround I developed. I first check if the shape is an image, and if it is, I write the image to a local directory. Then I add a picture to the slide using that saved image. Finally, I delete the locally saved image.

Now this copy_slide function works for images:

def copy_slide_from_external_prs(src, idx, newPrs):

# specify the slide you want to copy the contents from

src_slide = src.slides[idx]

# Define the layout you want to use from your generated pptx

SLD_LAYOUT = 5

slide_layout = prs.slide_layouts[SLD_LAYOUT]

# create now slide, to copy contents to

curr_slide = newPrs.slides.add_slide(slide_layout)

# create images dict

imgDict = {}

# now copy contents from external slide, but do not copy slide properties

# e.g. slide layouts, etc., because these would produce errors, as diplicate

# entries might be generated

for shp in src_slide.shapes:

if 'Picture' in shp.name:

# save image

with open(shp.name+'.jpg', 'wb') as f:

f.write(shp.image.blob)

# add image to dict

imgDict[shp.name+'.jpg'] = [shp.left, shp.top, shp.width, shp.height]

else:

# create copy of elem

el = shp.element

newel = copy.deepcopy(el)

# add elem to shape tree

curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')

# add pictures

for k, v in imgDict.items():

curr_slide.shapes.add_picture(k, v[0], v[1], v[2], v[3])

os.remove(k)

### 使用 `python-pptx` 库向 PPT 表格添加行 为了实现向现有的 Microsoft PowerPoint 文件中的表格添加新行的功能,可以利用 `python-pptx` 提供的操作方法。下面展示了一个具体的例子来说明这一过程。 首先需要安装并引入必要的库: ```python from pptx import Presentation from pptx.util import Inches, Pt ``` 定义一个函数用于打开指定路径下的 .pptx 文件,并定位到特定编号的幻灯片及其上的第一个表格对象,在该表的最后一行之后追加一行数据[^1]。 ```python def add_row_to_table(ppt_path, slide_index=0): # 打开演示文档 prs = Presentation(ppt_path) # 获取目标幻灯片 slide = prs.slides[slide_index] # 假设我们操作的是幻灯片上第一个形状为表格的情况 shape = slide.shapes[0] if not shape.has_table: raise ValueError("The selected shape is not a table.") table = shape.table # 添加新的一行至表格末尾 new_row_idx = len(table.rows) table.add_row() # 访问新增加的那一行的第一个单元格,并设置其文本内容 cell = table.cell(new_row_idx, 0) cell.text = 'New Row Content' # 如果有更多列,则继续填充其他单元格... for col in range(1, len(table.columns)): cell = table.cell(new_row_idx, col) cell.text = f'Col {col} Data' # 将更改保存回原始文件或另存为新的PowerPoint文件 prs.save(ppt_path) ``` 此代码片段展示了如何通过编程方式访问现有PPT内的表格结构,并在其基础上动态增加额外的数据行。注意这里假设了所选图形确实是表格;如果不是的话会抛出异常提示错误[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值