如果你还没有安装python-pptx
和python-docx
,请先运行以下命令:
pip install python-pptx python-docx
python
from pptx import Presentation
from docx import Document
import re
# 函数:清理文本,移除特殊字符和控制字符
def clean_text(text):
# 移除所有控制字符和特殊字符
cleaned_text = re.sub(r'[\x00-\x1F\x7F-\x9F]', '', text)
return cleaned_text
# 读取PPT文件
ppt_file = 'example.pptx'
presentation = Presentation(ppt_file)
# 创建Word文档
doc = Document()
for slide in presentation.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
# 清理文本内容
cleaned_text = clean_text(shape.text)
doc.add_paragraph(cleaned_text)
# 保存Word文档
doc_file = 'example.docx'
doc.save(doc_file)