Remove Attachments from PDF via Python

Delete Attachments from PDF document. Use Aspose.PDF for Python for .NET to modify PDF files programmatically

How to remove Attachments Using Python for .NET Library

Remove Attachments from PDF using the Aspose.PDF for Python via .NET. Files with attachments, such as images or other PDF, can increase the size of a document. Removing attachments may help reduce the file size, making it easier to share and store. Also, attachments can contain confidential information that you don’t want to share with others. So, delete attachments from PDF. Removing attachments helps protect your data. Malicious attachments may pose security risks. Removing unknown or untrusted attachments from PDF will help prevent potential security threats. Removing attachments makes the PDF easier to print and view your document. We can conclude, that removing attachments from PDF can make the document more user-friendly, efficient, and compatible. In order to remove Attachments in PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for python-net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Python Package Manager Console

pip install aspose-pdf

Remove Attachments from PDF


You need Aspose.PDF for Python via .NET to try the code in your environment.

  1. Open existing PDF.
  2. Delete all attachments.
  3. Save the result.

This code snippet programmatically removes all attachments from a PDF document and saves the modified PDF as a separate file. It initializes a ‘pdfDocument’ object, which is associated with the PDF file located at the ‘dataDir + DeleteAllAttachments.pdf’ path. After opening the PDF document, the code instructs the software to delete all attachments contained within this PDF file. Attachments in a PDF can include embedded files, such as images or other documents. The code modifies the ‘dataDir’ variable to store the path where the updated PDF file, without any attachments, will be saved. The updated PDF will be saved with the filename ‘DeleteAllAttachments_out.pdf.’

Delete Attachment from PDF document

This sample code shows how to Remove attachment from PDF - Python

    def attachment_remove(self, infile, outfile):

        # Open document
        path_infile = self.dataDir + infile
        path_outfile = self.dataDir + outfile
        pdfDocument = Document(path_infile)

        # Delete all attachments
        pdfDocument.EmbeddedFiles.Delete()

        # Save updated file
        pdfDocument.Save(path_outfile)