Extraia anexos do documento PDF via Python

Como extrair anexos de um PDF programaticamente com Python

Como extrair anexos usando a biblioteca Python for .NET

Para extrair anexos em arquivo PDF, usaremos a API Aspose.PDF for .NET, que é uma API de manipulação de documentos rica em recursos, poderosa e fácil de usar para a plataforma python-net. Abra o gerenciador de pacotes NuGet, procure por Aspose.pdf e instale. Você também pode usar o seguinte comando no Console do Gerenciador de Pacotes.

Python Package Manager Console

pip install aspose-pdf

Extrair anexos do PDF Python


Você precisa do Aspose.PDF para .NET para testar o código em seu ambiente.

  1. Obtenha a coleção de arquivos incorporados.
  2. Obtenha a contagem dos arquivos incorporados.
  3. Percorra a coleção para obter todos os anexos.
  4. Verifique se o objeto de parâmetro contém os parâmetros.
  5. Obtenha o Anexo e grave em arquivo ou stream.

Extrair anexo de um documento PDF

 def attachment_extract(self, infile):

        path_infile = self.dataDir + infile

        # Open document
        pdfDocument = Document(path_infile)

        # Get embedded files collection
        embeddedFiles = pdfDocument.EmbeddedFiles

        # Get count of the embedded files
        print ( "Total files : %d " % (embeddedFiles.Count))

        count = 1

        # Loop through the collection to get all the attachments

        for fileSpecification in embeddedFiles:
            print("Name: " + fileSpecification.Name)
            print("Description: " + fileSpecification.Description)
            print("Mime Type: " + fileSpecification.MIMEType)

            # Check if parameter object contains the parameters
            if (fileSpecification.Params != None):
                print("CheckSum: " + fileSpecification.Params.CheckSum)
                print("Creation Date: " + fileSpecification.Params.CreationDate)
                print("Modification Date " + fileSpecification.Params.ModDate)
                print("Size: " + fileSpecification.Params.Size)

                # Get the attachment and write to file or stream
                File.WriteAllBytes(self.dataDir + count + "_out" + ".txt", fileSpecification.Contents)

                count+=1