如何使用python将txt文件或PDF转换为Word doc?

问题描述 投票:-1回答:2

有没有办法将PDF(或文本文件)转换为python中的Word文档?我正在为我的教授做一些网络抓取,原始文档是PDF。我将所有1,611个转换为文本文件,现在我们需要将它们转换为Word文档。我唯一能找到的是一个Word-to-txt转换器,而不是相反。

谢谢!

python pdf ms-word converter
2个回答
2
投票

使用python-docx我能够很容易地将txt文件转换为Word文档。

这就是我做的。

from docx import Document
import re
import os

path = '/users/tdobbins/downloads/smithtxt'
direct = os.listdir(path)

for i in direct:
    document = Document()
    document.add_heading(i, 0)
    myfile = open('/path/to/read/from/'+i).read()
    myfile = re.sub(r'[^\x00-\x7F]+|\x0c',' ', myfile) # remove all non-XML-compatible characters
    p = document.add_paragraph(myfile)
    document.save('/path/to/write/to/'+i+'.docx')

1
投票

你可以看看python-docx。它可以使用python创建Word文档,因此您可以将文本文件存储到单词中。见python-docx - what-it-can-do

© www.soinside.com 2019 - 2024. All rights reserved.