Преобразование .pdf в .docx в API служб Adobe pdf (с использованием Python)
Я пытаюсь (пока 2 дня) написать программу на Python, конвертирующую файлы «.pdf» в «.docx» , используя API сервера Adobe pdf (бесплатная пробная версия).
Я нашел литературу, позволяющую преобразовать любой файл ".pdf" в файл ".zip", содержащий файлы ".txt" (восстановление текстовых данных) и файлы ".excel" (возвращение табличных данных).
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".
- Вы когда-нибудь сталкивались с такой проблемой? может кто-нибудь помочь? Спасибо !
1 ответ
К сожалению, сейчас Python SDK поддерживает только часть извлечения наших служб PDF. В качестве альтернативы вы можете использовать службы через REST API (https://documentcloud.adobe.com/document-services/index.html#how-to-get-started-).