当前位置: 首页 > news >正文

两个还算好用的ppt转word和PDF转word的python脚本

PPT转word:

import re
from pptx import Presentation
from docx import Document
from docx.shared import Inches
from io import BytesIO
from PIL import Image

def clean_text(text):
    # 使用正则表达式删除控制字符和NULL字节
    return re.sub(r'[\x00-\x1F\x7F]', '', text)

def ppt_to_word(ppt_file, word_file):
    prs = Presentation(ppt_file)
    doc = Document()

    for slide in prs.slides:
        for shape in slide.shapes:
            if shape.has_text_frame:
                text = shape.text_frame.text
                cleaned_text = clean_text(text)  # 清理文本中的无效字符
                doc.add_paragraph(cleaned_text)

            # shape.shape_type==13 表示图片
            if shape.shape_type == 13:
                image = shape.image
                # 使用图片原始的 blob 数据构建 BytesIO
                image_stream = BytesIO(image.blob)
                # 插入图片到Word文档
                doc.add_paragraph().add_run().add_picture(image_stream, width=Inches(3))

    doc.save(word_file)
    print(f"转换完成,文件保存为 {word_file}")

# 示例
ppt_to_word("PPTs/Fixed Asset.pptx", "Documents/fixed asset.docx")

PDF转word:

import sys
import PyPDF2
from docx import Document

def extract_text_from_pdf(pdf_path):
    """
    从 PDF 文件中提取文本内容
    :param pdf_path: PDF 文件的路径
    :return: 提取的文本,字符串类型
    """
    text = ""
    with open(pdf_path, 'rb') as file:
        reader = PyPDF2.PdfReader(file)
        # 遍历每一页,并提取文字
        for page in reader.pages:
            page_text = page.extract_text()
            if page_text:
                text += page_text + "\n"
    return text

def write_text_to_docx(text, docx_path):
    """
    将文本写入到 Word 文档中
    :param text: 需要写入的文本内容
    :param docx_path: 输出 docx 文件的路径
    """
    document = Document()
    document.add_paragraph(text)
    document.save(docx_path)

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("用法: python extract_pdf_text.py 输入文件.pdf [输出文件.docx]")
        sys.exit(1)

    pdf_path = sys.argv[1]
    # 如果传入了输出文件名则使用,否则默认 "output.docx"
    output_path = sys.argv[2] if len(sys.argv) > 2 else "output.docx"

    try:
        print("正在提取 PDF 文本...")
        text = extract_text_from_pdf(pdf_path)
        print("正在写入到 Word 文档...")
        write_text_to_docx(text, output_path)
        print(f"转换成功!输出文档为:{output_path}")
    except Exception as e:
        print("转换失败:", e)

相关文章:

  • 执行adb指令报错:error: more than one device/emulator原因及解决方法
  • 构建高效的LinkedIn图像爬取工具
  • 如何解释storefile文件的合并和分裂?
  • 利用 Agent TARS 技术实现互联网舆情监测与事件自动化创建的可行性与前景
  • 内网(域)渗透测试流程和模拟测试day--1--信息收集阶段
  • DeiT:数据高效的图像Transformer及其工作原理详解
  • 【2025】基于springboot+vue的医院在线问诊系统设计与实现(源码、万字文档、图文修改、调试答疑)
  • 【详细解决】pycharm 终端出现报错:“Failed : 无法将“Failed”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
  • python项目使用docker在linux系统离线部署
  • 关于网络的一点知识(持续更新)
  • sqlite3数据库(文件)损坏恢复方法
  • 安恒春招一面
  • Delta Lake 解析:架构、数据处理流程与最佳实践
  • 【C++】类和对象(匿名对象)
  • Python爬虫-爬取AliExpress商品搜索词排名数据
  • Python FastAPI面试题及参考答案
  • 基于springboot的新闻推荐系统(045)
  • Mybatis_plus
  • 数据库三级填空+应用题(1)
  • 【Node.js入门笔记10---http 模块】
  • 王受文已任全国工商联党组成员
  • 江苏省人社厅党组书记、厅长王斌接受审查调查
  • 读图|展现城市品格,上海城市影像走进南美
  • 苹果Safari浏览器上的搜索量首次下降
  • “半世纪来对无争议边界最深入袭击”:印巴冲突何以至此又如何收场?
  • 央行行长详解降息:将通过利率自律机制引导商业银行相应下调存款利率