【问题标题】:How to convert txt file or PDF to Word doc using python?如何使用 python 将 txt 文件或 PDF 转换为 Word doc?
【发布时间】:2015-06-01 09:03:36
【问题描述】:

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

谢谢!

【问题讨论】:

  • 您可以使用 Word 打开纯文本文件。
  • @TigerhawkT3 但是他将如何转换它们呢?打开 1611 个文件会很紧张。
  • @TigerhawkT3 因为,再一次,他们有 1,611 个。
  • 将纯文本文件转换为 Word 文档不会增加任何价值。它们已经可以在 Word 中打开,然后在必要时在 Word 中修改和重新保存。如果您只是希望文件在打开时在 Word 中启动,您只需为它们提供 .doc 扩展名(假设 Word 是用户对具有 .doc 扩展名的文件的默认应用程序)。
  • 我不同意。任何类型的自动化都具有巨大的价值。你写过文档吗?如果你有,你应该知道它是多么令人麻木。这就是很棒的地方!

标签: python pdf ms-word converter


【解决方案1】:

使用 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')

【讨论】:

  • 我不想通过我的代码进行格式化。有什么方法可以接受输入文件并将其转换为 .doc/.docx?
【解决方案2】:

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

【讨论】:

  • 谢谢。我正在检查它。除了安装它很痛苦之外,它看起来会起作用。
  • 我不想通过我的代码进行格式化。有什么方法可以接受输入文件并将其转换为 .doc/.docx?
【解决方案3】:

您可以使用 GroupDocs.Conversion Cloud,它提供 Python SDK 用于将文本/PDF 转换为 DOC/DOCX 以及许多其他常见的文件格式,从一种格式到另一种格式,无需依赖任何第三方工具或软件。

这是示例 Python 代码。

# Import module
import groupdocs_conversion_cloud

# Get your app_sid and app_key at https://dashboard.groupdocs.cloud (free registration is required).
app_sid = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"
app_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# Create instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(app_sid, app_key)
file_api = groupdocs_conversion_cloud.FileApi.from_keys(app_sid, app_key)

try:

        #upload soruce file to storage
        filename = 'Sample.pdf'
        remote_name = 'Sample.pdf'
        output_name= 'sample.doc'
        strformat='doc'

        request_upload = groupdocs_conversion_cloud.UploadFileRequest(remote_name,filename)
        response_upload = file_api.upload_file(request_upload)
        #Convert PDF to Word document
        settings = groupdocs_conversion_cloud.ConvertSettings()
        settings.file_path =remote_name
        settings.format = strformat
        settings.output_path = output_name

        loadOptions = groupdocs_conversion_cloud.PdfLoadOptions()
        loadOptions.hide_pdf_annotations = True
        loadOptions.remove_embedded_files = False
        loadOptions.flatten_all_fields = True

        settings.load_options = loadOptions

        convertOptions = groupdocs_conversion_cloud.DocxConvertOptions()
        convertOptions.from_page = 1
        convertOptions.pages_count = 1

        settings.convert_options = convertOptions
 .               
        request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
        response = convert_api.convert_document(request)

        print("Document converted successfully: " + str(response))
except groupdocs_conversion_cloud.ApiException as e:
        print("Exception when calling get_supported_conversion_types: {0}".format(e.message))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多