Add Attachments
IronPDF for Python can embed file attachments within a PDF document and remove them programmatically, using the Attachments collection on any PdfDocument object.
Getting Started
Load or render a PdfDocument, then use Attachments.AddAttachment to embed binary data as a named attachment. Use Attachments.RemoveAttachment to remove an attachment by reference.
Understanding the Code
pdf.Attachments.AddAttachment(name, data): Embeds a file attachment in the PDF with the given name. The second argument accepts abyte[](binary data). In this example,pdf.BinaryDataprovides the raw bytes of a second loadedPdfDocument.- Returns an attachment reference object that can be used later with
RemoveAttachment.
- Returns an attachment reference object that can be used later with
my_pdf.Attachments.RemoveAttachment(attachment): Removes a previously added attachment from the document using the reference returned byAddAttachment.SaveAs(path): Saves the final document, with all attachments embedded or removed as configured.
Use Cases
Attachments are useful for including source data alongside a report PDF — for example, embedding the raw spreadsheet, XML data file, or original HTML template that was used to generate the document. Embedded attachments are portable and travel with the PDF file.






