Remove Attachments from PDF in Python
Contents
[
Hide
]
Aspose.PDF for Python can remove attachments from PDF files. A PDF document’s attachments are held in the Document object’s EmbeddedFiles collection.
This workflow is useful when you need to clean up outdated embedded files, reduce package size, or prepare a PDF for redistribution without attached source materials.
To delete all attachments associated with a PDF file:
- Call the EmbeddedFiles collection’s delete() method.
- Save the updated file using the Document object’s save() method.
The following code snippet shows how to remove attachments from a PDF document.
import aspose.pdf as ap
def remove_attachment(infile, outfile):
# Open PDF document
with ap.Document(infile) as document:
document.embedded_files.delete()
document.save(outfile)
Remove a specific attachment by name
If you need to remove only one attachment and keep the others, use the delete_by_key() method and pass the attachment name.
To delete a specific attachment:
- Open the source PDF file.
- Call
document.embedded_files.delete_by_key(attachment_name). - Save the updated PDF file.
The following code snippet removes one attachment by its name.
import aspose.pdf as ap
def remove_attachment(infile, attachment_name, outfile):
# Open PDF document
with ap.Document(infile) as document:
document.embedded_files.delete_by_key(attachment_name)
document.save(outfile)