This short tutorial explains how to insert PDF into Word document using Python. It provides the environment setup, a sequence of instructions, and a Python script that shows how to attach a PDF to Word using Python. You will also find API options to change the insertion point and the icon, and to embed files other than PDF.
Steps to Embed a PDF in Word using Python
- Install Aspose.Words for Python via .NET in the development environment to add PDF attachments to Word files
- Import the aspose.words module and load the existing Word file into the Document class
- Use a DocumentBuilder to write a short line of text above the attachment
- Open the PDF file in binary read mode
- Insert the PDF stream as an icon with a caption by calling the insert_ole_object_as_icon() method
- Update the file name and display name in the package of the embedded PDF
- Save the resultant Word file containing the attached PDF
These steps outline the process to add PDF to Word using Python. Import the aspose.words module, load the Word document, write a line of text with the DocumentBuilder, and open the PDF file in binary read mode. The insert_ole_object_as_icon() method receives this file stream and inserts the PDF as an icon, and the file name and display name of the package are updated before the final Word file is saved.
Code to Attach a PDF File to a Word Document using Python
This Python script demonstrates how to attach a document in Word using Python. Call move_to_bookmark() or move_to_cell() before the insertion to place the PDF at a bookmark or inside a table cell rather than at the start of the document. Replace None with the path of an ICO file in the insert_ole_object_as_icon() call to display your own icon. In the same way, Excel, PowerPoint, ZIP, or other Word files can be embedded by changing the input file.
This article explained the process of embedding a PDF in a Word file. For deleting bookmarks from a Word document, refer to the article Remove Bookmark in Word using Python.