我有数百个演示文稿。其中我需要更改每个文本的颜色和字体以及每张幻灯片的背景颜色。这在Python中如何实现呢? 我尝试了解 pptx 模块,但在 Youtube 上没有找到合适的视频。如何更改现有演示文稿中先前列出的参数?
解决方案:
想法是循环加载每个文件名,处理更改,然后保存新文件。当前设置为保存现有文件,因此您可能需要更改该文件或制作当前文件的备份副本。我对此进行了测试,它按预期工作。
from pptx import Presentation
from pptx.dml.color import RGBColor
file_list = ["file1.pptx", "file2.pptx"]
for file_name in file_list:
# Open the current file
ppt = Presentation(file_name)
# Get all slides in the presentations
slides = ppt.slides
# Loop over each slide to process what's inside
for slide in slides:
# Access slide background and fill with solid color
background = slide.background
fill = background.fill
fill.solid()
fill.fore_color.rgb = RGBColor(0, 0, 255)
# Get list of all shapes in slide
for shape in slide.shapes:
# If this is a text box then access the text and change the color
if not shape.has_text_frame:
continue
text_frame = shape.text_frame
for p in text_frame.paragraphs:
for run in p.runs:
font = run.font
font.color.rgb = RGBColor(255, 0, 0)
# Save over the existing file
ppt.save(file_name)