Python を介して PDF ドキュメントから添付ファイルを抽出

Python を使用してPDFから添付ファイルをプログラムで抽出する方法

Python for .NET ライブラリを使用して添付ファイルを抽出する方法

添付ファイルをPDFファイルに抽出するために、python-net プラットフォーム用の機能豊富で強力で使いやすいドキュメント操作APIである Aspose.PDF for .NET APIを使用します。NuGet パッケージマネージャーを開き、aspose.pdf を検索してインストールします。パッケージマネージャーコンソールから次のコマンドを使用することもできます。

Python Package Manager Console

pip install aspose-pdf

PDF Python から添付ファイルを抽出


ご使用の環境でコードを試すには Aspose.PDF for .NET が必要です。

1。埋め込みファイルコレクションを取得します。 1。埋め込みファイルの数を取得します。 1。コレクションをループして、すべての添付ファイルを取得します。 1。パラメーターオブジェクトにパラメーターが含まれているかどうかを確認します。 1。添付ファイルを取得し、ファイルまたはストリームに書き込みます。

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