HTML JPG PDF XML DOCX
  Product Family
PDF

Extrair Anexos de PDF no Python SDK

API para extrair anexos em documentos PDF com Aspose.PDF Cloud Python SDK

Get Started

Como extrair anexos de documentos PDF usando o Cloud Python SDK

Para trabalhar com anexos em documentos PDF, usaremos Aspose.PDF Cloud Python SDK Este SDK em nuvem ajuda programadores Python a desenvolver aplicativos baseados em nuvem para criar, anotar, editar e converter PDFs usando a linguagem de programação Python via Aspose.PDF REST API. Abra NPM gerenciador de pacotes, procure por Aspose.PDF Cloud, e instale. Você também pode usar o seguinte comando a partir do Console do Gerenciador de Pacotes.

Instalação do Github


     
    pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git

Comando do Console do Gerenciador de Pacotes

     
    pip install asposepdfcloud

Passos para obter anexos via Python SDK

Os desenvolvedores do Aspose.PDF Cloud Python podem facilmente extrair anexos de documentos PDF. Os desenvolvedores precisam de apenas algumas linhas de código.

  1. Carregue seu Segredo da Aplicação e Chave do arquivo JSON ou configure as credenciais de outra forma
  2. Crie um objeto para se conectar à API de Nuvem
  3. Defina o nome com o caminho do arquivo do seu documento
  4. Execute as ações de extração de anexos
  5. A resposta da função da API de Nuvem conterá uma lista de anexos

Requisitos do Sistema

É fácil começar a usar o Aspose.PDF Cloud Python SDK e não há nada para instalar. Basta criar uma conta na Aspose for Cloud e obter as informações da sua aplicação. Assim que tiver o App SID & chave, você estará pronto para usar o Aspose.PDF Cloud Python SDK.

  • CPU: 1GHz
  • RAM: 512Mb
  • Espaço livre em disco: 20Mb
  • Qualquer SO Microsoft Windows, Mac OS, Linux x32/x64
  • Python v4.8 ou superior

 

Este código de exemplo mostra a extração de todos os anexos do documento PDF usando o PDF Cloud Python SDK


    import shutil
    import json
    import logging
    from pathlib import Path
    from asposepdfcloud import ApiClient, PdfApi, AttachmentsResponse, AttachmentResponse, Attachment

    # Configure logging
    logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

    class Config:
        """Configuration parameters."""
        CREDENTIALS_FILE = Path(r"C:\\Projects\\ASPOSE\\Pdf.Cloud\\Credentials\\credentials.json")
        LOCAL_FOLDER = Path(r"C:\Samples")
        PDF_DOCUMENT_NAME = "sample_file_with_attachment.pdf"
        ATTACHMENT_PATH = ""

    class PdfAttachments:
        """Class for managing PDF attachments using Aspose PDF Cloud API."""
        def __init__(self, credentials_file: Path = Config.CREDENTIALS_FILE):
            self.pdf_api = None
            self._init_api(credentials_file)

        def _init_api(self, credentials_file: Path):
            """Initialize the API client."""
            try:
                with credentials_file.open("r", encoding="utf-8") as file:
                    credentials = json.load(file)
                    api_key, app_id = credentials.get("key"), credentials.get("id")
                    if not api_key or not app_id:
                        raise ValueError("init_api(): Error: Missing API keys in the credentials file.")
                    self.pdf_api = PdfApi(ApiClient(api_key, app_id))
            except (FileNotFoundError, json.JSONDecodeError, ValueError) as e:
                logging.error(f"init_api(): Failed to load credentials: {e}")

        def upload_document(self):
            """Upload a PDF document to the Aspose Cloud server."""
            if self.pdf_api:
                file_path = Config.LOCAL_FOLDER / Config.PDF_DOCUMENT_NAME
                try:
                    self.pdf_api.upload_file(Config.PDF_DOCUMENT_NAME, str(file_path))
                    logging.info(f"upload_document(): File {Config.PDF_DOCUMENT_NAME} uploaded successfully.")
                except Exception as e:
                    logging.error(f"upload_document(): Failed to upload file: {e}")

        def get_attachments(self):
            """Get attachments for the PDF document."""
            if self.pdf_api:
                try:
                    response : AttachmentsResponse = self.pdf_api.get_document_attachments(Config.PDF_DOCUMENT_NAME)
                    if response.code == 200:
                        logging.info(f"get_attachmnets(): attachments '{response.attachments}' for the document '{Config.PDF_DOCUMENT_NAME}'.")
                        Config.ATTACHMENT_PATH = response.attachments.list[0].links[0].href
                    else:
                        logging.error(f"get_attachmnets(): Failed to get attachments to the document. Response code: {response.code}")
                except Exception as e:
                    logging.error(f"get_attachmnets(): Error while adding attachment: {e}")

        def get_attachment_by_id(self):
            """Get attachment by Id for the PDF document and save it to local file."""
            if self.pdf_api:
                try:
                    response : AttachmentResponse = self.pdf_api.get_document_attachment_by_index(Config.PDF_DOCUMENT_NAME, Config.ATTACHMENT_PATH)
                    if response.code == 200:
                        attachment: Attachment = response.attachment
                        temp_file = self.pdf_api.get_download_document_attachment_by_index(Config.PDF_DOCUMENT_NAME, Config.ATTACHMENT_PATH)
                        local_path = Config.LOCAL_FOLDER / attachment.name
                        shutil.copy(temp_file, local_path)
                        logging.info(f"get_attachment_by_id(): attachment '{local_path}' for the document '{Config.PDF_DOCUMENT_NAME}' successfuly saved.")
                    else:
                        logging.error(f"get_attachment_by_id(): Failed to get attachment for the document '{Config.PDF_DOCUMENT_NAME}'. Response code: {response.code}")
                except Exception as e:
                    logging.error(f"get_attachment_by_id(): Error while get attachment: {e}")


    if __name__ == "__main__":
        pdf_attachments = PdfAttachments()
        pdf_attachments.upload_document()
        pdf_attachments.get_attachments()
        pdf_attachments.get_attachment_by_id()