Instantiate the Viewer object. Specify a file that contains attachments.
Call the get_attachments method. It returns the attachment collection.
Iterate through the collection. To save an attachment, call the save_attachment method.
The following code snippet shows how to get and save all attachments from the MSG file:
Note
NOTE: provided code snippet suits all format families that support attachments: emails, Outlook data files, archives, and PDF documents.
fromgroupdocs.viewerimportViewerdefextract_and_save_attachments():# Load document with attachmentswithViewer("with_attachments.msg")asviewer:attachments=viewer.get_attachments()print("\nAttachments:")forattachmentinattachments:print(attachment)# Save attachment to diskviewer.save_attachment(attachment,f"./attachments/{attachment.file_name}")print(f"\nAttachments retrieved successfully.")if__name__=="__main__":extract_and_save_attachments()
with_attachments.msg is the sample file used in this example. Click here to download it.