帮我写一个python代码,要求将指定文件夹的ppt转换为图片,并用ppt原文件命名,保存的指定文文件夹
时间: 2023-04-01 19:00:22 浏览: 208
很高兴为您服务!以下是您需要的 Python 代码:
```python
import os
from pptx import Presentation
# 指定文件夹路径和保存路径
folder_path = "指定文件夹路径"
save_path = "保存的指定文件夹路径"
# 遍历文件夹中的 ppt 文件
for file_name in os.listdir(folder_path):
if file_name.endswith(".ppt") or file_name.endswith(".pptx"):
# 读取 ppt 文件
prs = Presentation(os.path.join(folder_path, file_name))
# 遍历 ppt 中的每一页
for i, slide in enumerate(prs.slides):
# 将每一页保存为图片
image_name = os.path.splitext(file_name)[] + "_slide{}.png".format(i+1)
slide.shapes.title.text = image_name
slide.shapes.title.text_frame.paragraphs[].font.size = 14
slide.shapes.title.text_frame.paragraphs[].font.color.rgb = (, , )
slide.shapes.title.text_frame.paragraphs[].font.bold = True
slide.shapes.title.text_frame.paragraphs[].font.name = '微软雅黑'
slide.shapes.title.text_frame.paragraphs[].font.underline = False
slide.shapes.title.text_frame.paragraphs[].font.italic = False
slide.shapes.title.text_frame.paragraphs[].font.shadow = False
slide.shapes.title.text_frame.paragraphs[].font.highlight_color.rgb = (, , )
slide.shapes.title.text_frame.paragraphs[].font.language_id = 2052
slide.shapes.title.text_frame.paragraphs[].font.font_style = 'normal'
slide.shapes.title.text_frame.paragraphs[].font.font_color.rgb = (, , )
slide.shapes.title.text_frame.paragraphs[].font.fill.background()
slide.shapes.title.text_frame.paragraphs[].font.fill.solid()
slide.shapes.title.text_frame.paragraphs[].font.fill.fore_color.rgb = (255, 255, 255)
slide.shapes.title.text_frame.paragraphs[].font.fill.transparency =
slide.shapes.title.text_frame.paragraphs[].font.fill.type = 1
slide.shapes.title.text_frame.paragraphs[].font.fill._element.getparent().remove(slide.shapes.title.text_frame.paragraphs[].font.fill._element)
slide.shapes.title.text_frame.paragraphs[].font.fill._element = None
slide.shapes.title.text_frame.paragraphs[].font._element.getparent().remove(slide.shapes.title.text_frame.paragraphs[].font._element)
slide.shapes.title.text_frame.paragraphs[].font._element = None
slide.shapes.title.text_frame.paragraphs[]._element.getparent().remove(slide.shapes.title.text_frame.paragraphs[]._element)
slide.shapes.title.text_frame.paragraphs[]._element = None
slide.shapes.title._element.getparent().remove(slide.shapes.title._element)
slide.shapes.title._element = None
slide.shapes._element.getparent().remove(slide.shapes._element)
slide.shapes._element = None
slide._element.getparent().remove(slide._element)
slide._element = None
slide.export(os.path.join(save_path, image_name), "png")
```
请将代码中的 "指定文件夹路径" 和 "保存的指定文件夹路径" 替换为您实际的文件夹路径和保存路径。此代码将遍历指定文件夹中的 ppt 文件,并将每一页保存为一张图片,图片名称为原 ppt 文件名加上 "_slideX.png",其中 X 为页码。图片将保存在指定的保存路径中。
阅读全文