在AdobePDF服务api上将.pdf转换为.docx(使用python)

olqngx59  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(303)

我正在尝试(到目前为止已有3天)编写一个python程序,使用AdobePDF服务器api(免费试用)将“.pdf”文件转换为“.docx”文件。
我发现有文献支持将任何.pdf文件转换为包含.txt文件(恢复文本数据)和.excel文件(返回表格数据)的.zip文件。

import logging
import os.path

from adobe.pdfservices.operation.auth.credentials import Credentials
from adobe.pdfservices.operation.exception.exceptions import ServiceApiException, ServiceUsageException, SdkException
from adobe.pdfservices.operation.pdfops.options.extractpdf.extract_pdf_options import ExtractPDFOptions
from adobe.pdfservices.operation.pdfops.options.extractpdf.extract_element_type import ExtractElementType
from adobe.pdfservices.operation.execution_context import ExecutionContext
from adobe.pdfservices.operation.io.file_ref import FileRef
from adobe.pdfservices.operation.pdfops.extract_pdf_operation import ExtractPDFOperation

logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))

try:
    # get base path.
    base_path =os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath("C:/..link.../extractpdf/extract_txt_from_pdf.ipynb"))))

    # Initial setup, create credentials instance.
    credentials = Credentials.service_account_credentials_builder()\
        .from_file(base_path + "\\pdfservices-api-credentials.json") \
        .build()

    #Create an ExecutionContext using credentials and create a new operation instance.
    execution_context = ExecutionContext.create(credentials)
    extract_pdf_operation = ExtractPDFOperation.create_new()

    #Set operation input from a source file.
    source = FileRef.create_from_local_file(base_path + "/resources/trs_pdf_file.pdf")
    extract_pdf_operation.set_input(source)

    # Build ExtractPDF options and set them into the operation
    extract_pdf_options: ExtractPDFOptions = ExtractPDFOptions.builder() \
        .with_element_to_extract(ExtractElementType.TEXT) \
        .with_element_to_extract(ExtractElementType.TABLES) \
        .build()
    extract_pdf_operation.set_options(extract_pdf_options)

    #Execute the operation.
    result: FileRef = extract_pdf_operation.execute(execution_context)

    # Save the result to the specified location.
    result.save_as(base_path + "/output/Extract_TextTableau_From_trs_pdf_file.zip")
except (ServiceApiException, ServiceUsageException, SdkException):
    logging.exception("Exception encountered while executing operation")

但在将提取文件的名称更改为之后,我还无法将其转换为“.docx”文件 name.docx 我去看了那本书 adobe.pdfservices.operation.pdfops.options.extractpdf.extract_pdf_options.ExtractPDFOptions() 但是没有找到调整提取并将其从“.zip”更改为“.docx”的方法。
你曾经遇到过这样的问题吗。有人能帮忙吗?非常感谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题