Trabajar con archivos adjuntos en documentos PDF a través de Python

Cómo obtener, agregar, guardar y eliminar archivos adjuntos de un PDF mediante programación con Python

Cómo gestionar los archivos adjuntos mediante la biblioteca de Python for .NET

Para agregar archivos adjuntos en un archivo PDF, usaremos la API Aspose.PDF for .NET, que es una API de manipulación de documentos rica en funciones, potente y fácil de usar para la plataforma python-net. Abra el administrador de paquetes NuGet, busque Aspose.pdf e instálelo. También puede usar el siguiente comando desde la consola de Package Manager.

Python Package Manager Console

pip install aspose-pdf

Trabajar con archivos adjuntos en PDF a través de Python


Necesita Aspose.PDF para .NET para probar el código en su entorno.

  1. Crea un nuevo proyecto de Python.
  2. Agregue una referencia a la DLL Aspose.PDF.
  3. Crea un objeto Document.
  4. Cree un objeto FileSpecification con el archivo que va a agregar y la descripción del archivo.
  5. Agregue el objeto FileSpecification a la colección EmbeddedFiles del objeto Document, con el método Add de la colección
  6. Guarde el archivo PDF.

Agregar un archivo adjunto a un documento PDF

    def attachment_add(self, infile, outfile):

        path_infile = self.dataDir + infile
        path_outfile = self.dataDir + outfile

        # Open document
        pdfDocument = Document(path_infile)

        # Setup new file to be added as attachment
        fileSpecification = FileSpecification(self.dataDir  + "test.txt", "Sample text file")

        # Add attachment to document's attachment collection
        pdfDocument.EmbeddedFiles.Add(fileSpecification)

        # Save new output
        pdfDocument.Save(path_outfile)