Python从PPT文件中提取所有文字到Word

1|0需求

将PPT文件或PPTX文件里面的所有文字提取到一个新的以docx结尾的Word文件中。

2|0安装Python库

(1)基于Python 3

(2) 运行下方代码安装需要用到的库

pip install python-pptx pip install python-docx

3|0执行下方Python代码

import collections import collections.abc import sys import os from pptx import Presentation import docx def extract_text(input_file, output_file): prs = Presentation(input_file) doc = docx.Document() for slide in prs.slides: for shape in slide.shapes: if not shape.has_text_frame: continue for paragraph in shape.text_frame.paragraphs: for run in paragraph.runs: doc.add_paragraph(run.text) doc.save(output_file) if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: python main.py <input_file> <output_file>") sys.exit() input_file = sys.argv[1] output_file = sys.argv[2] if not input_file.endswith(".pptx") or not output_file.endswith(".docx"): print("Error: input file must be .pptx and output file must be .docx") sys.exit() if not os.path.exists(input_file): print("Error: input file does not exist") sys.exit() extract_text(input_file, output_file) print("Text extracted successfully!")



作者:艾孜尔江·艾尔斯兰


__EOF__

本文作者艾孜尔江
本文链接https://www.cnblogs.com/ezhar/p/17121363.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   艾孜尔江  阅读(210)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-02-15 艾孜尔江关于《胡同的记忆》手游联网模块给出的简单建议
点击右上角即可分享
微信分享提示