This article explains how to insert a PDF as an image into Word in Python. It has details to set the IDE, a list of programming steps, and sample code to insert PDF into Word as image in Python. You will also get a few suggestions to improvise the code for getting a custom output Word file.
Steps to Insert PDF Image into Word in Python
- Set the environment to use Aspose.Total for Python via .NET to render PDF to a Word file
- Apply the respective product license to enable full product features
- Load the source PDF file into memory using the Document class
- Create a new empty Word file for saving the resultant PDF images
- Set parameters such as resolution to render the PDF page to PNG
- Go through each PDF page one by one
- Transform each page to a PNG image and render it to a new page in the Word file by adding a page break
- Save the completed Word file to the disk
These steps describe how to insert a PDF image into Word in Python. To perform this task, load the source PDF file, create a new DOCX file, set the parameters for rendering the PDF page, and parse through each page in the PDF. Convert each PDF page to PNG with the defined settings, insert each resulting image to the new Word file, insert a page-break after each image, and continue the process until all the PDF page images are saved in the Word file.
Code for Inserting PDF into Word as Image in Python
This code demonstrates the process to insert PDF as image in Word in Python. The default scale for image rendering is 100%; however, you may insert each image with explicit height and width by using the number of points value from the page.media_box.width/height. If the PDF has mixed page sizes or orientations, you may consider using the aw.BreakType.SECTION_BREAK_NEW_PAGE with per-section page setup instead of a plain page break.
This guide shows the process to render a PDF file with text to a Word file as images, page by page. To convert a scanned PDF to a DOCX file, refer to the article Convert scanned PDF to Word using Python.