从 pptx、ppt、docx、doc 和 msg 文件中提取文本 python windows

extract text from pptx, ppt, docx, doc and msg files python windows

有没有办法从 windows 机器上的 pptx、ppt、docx、doc 和 msg 文件中提取文本?我有几百个这样的文件,需要一些编程方式。我更喜欢 Python。但我愿意接受其他建议

我在网上搜索并看到了一些讨论,但它们适用于 linux 机器

单词

我尝试用 python-docx 找词,要安装它,请编写 pip install python-docx。我有一个名为 example 的 word 文档,其中有 4 行文本,这些文本是以正确的方式抓取的,就像您在下面的输出中看到的那样。

from docx import Document

d = Document("example.docx")

for par in d.paragraphs:
    print(par.text)

输出(example.docx内容):

Titolo
Paragrafo 1 a titolo di esempio
This is an example of text
This is the final part, just 4 rows

将所有docx文本加入一个文件夹

import os
from docx import Document

files = [f for f in os.listdir() if ".docx" in f]
text_collector = []
whole_text = ''
for f in files:
    doc = Document(f)
    for par in doc.paragraphs:
        text_collector.append(par.text)

for text in text_collector:
    whole_text += text + "\n"

print(whole_text)

同上,但有选择

在此代码中,系统会要求您从文件夹中出现的 docx 文件列表中选择要加入的文件。

import os
from docx import Document

files = [f for f in os.listdir() if ".docx" in f]

for n,f in enumerate(files):
    print(n+1,f)
print()
print("Write the numbers of files you need separated by space")
inp = input("Which files do you want to join?")

desired = (inp.split())
desired = map(lambda x: int(x), desired)
list_to_join = []
for n in desired:
    list_to_join.append(files[n-1])


text_collector = []
whole_text = ''
for f in list_to_join:
    doc = Document(f)
    for par in doc.paragraphs:
        text_collector.append(par.text)

for text in text_collector:
    whole_text += text + "\n"

print(whole_text)