簡體   English   中英

Python 3.6從PPT文件中提取文本

[英]Python 3.6 Extracting text from PPT files

我正在使用textract python-pptx提取文件的文本內容,效果很好。 不幸的是,我們的客戶端也有需要處理的ppt文件,但是服務器中沒有任何MS Office / Open Office,因此我無法使用comtypes將ppt文件轉換為另一種文件類型,而只是執行從那里提取。

非常感謝其他方法的建議。

我在Windows 64位計算機上運行Python 3.6。

在這里轉換。 https://convertio.co/ppt-pptx/這將使您可以在程序中使用它們。

    from os.path import isfile, join
    import os
    import re
    from pptx import Presentation

   def getPptContent(path):
      prs = Presentation(path)
    text_runs = []
    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:
                   text_runs.append(run.text)
    return text_runs




ppt_dir = "ppt_data"

corpus = [str(f) for f in os.listdir(ppt_dir) if not f.startswith('.') and isfile(join(ppt_dir, f))]

for filename in corpus:
    Path = ppt_dir + "/" +filename
    print(Path)
    file_content = getPptContent(Path)
    f = open(ppt_dir + "/output/" + filename.split(".")[0]  +".txt" ,"w+", encoding="utf-8")
    f.write(str(file_content))
    f.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM