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

tmt*_*mes 3 python pdf ms-word converters

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

谢谢!

tmt*_*mes 7

使用 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')
Run Code Online (Sandbox Code Playgroud)