The python-docx module allows you to create, update, and extract information from Microsoft Word (.docx) files. One of its features is the ability to work with images. In this guide, I'll explain how you can add and manipulate images in a Word document using the python-docx module.
Before you can use the python-docx module, you need to install it:
pip install python-docx
To add an image to a Word document:
from docx import Document from docx.shared import Inches # Create a new Document doc = Document() # Add an image to the document doc.add_picture('path_to_image.jpg', width=Inches(2.0)) # Save the document doc.save('document_with_image.docx') In the above example, the add_picture method is used to insert an image into the document. The width argument is used to specify the width of the image. The Inches function is a helper to specify dimensions in inches. You can also use Cm for centimeters or omit the size argument to use the image's original size.
When you add an image, you can specify its width, height, or both. If you specify one dimension (e.g., width), the other dimension (e.g., height) will be computed to maintain the image's aspect ratio.
# Adding an image with a specific width and height doc.add_picture('path_to_image.jpg', width=Inches(2.0), height=Inches(1.5)) By default, images are added as inline shapes in the document. However, you can manipulate images as floating shapes using additional functionality:
from docx.enum.shapes import MSO_SHAPE # Add an image and get the shape reference shape = doc.add_picture('path_to_image.jpg', width=Inches(2.0)).shape # Set image layout to "floating" shape.wrap_format.type = shape.wrap_format.WRAP_NONE # Set position from the top and left of the page shape.left = Inches(1.0) shape.top = Inches(2.0) # Apply a predefined shape style (e.g., a rounded rectangle) shape.auto_shape_type = MSO_SHAPE.ROUNDED_RECTANGLE This will make the image float at a specific position in the document.
To extract images from an existing document:
from docx import Document doc = Document('document_with_image.docx') for rel in doc.part.rels.values(): if "image" in rel.reltype: image = rel.target_part.blob with open(f"extracted_{rel.target_ref}", "wb") as img_file: img_file.write(image) This script will extract all the images from the document and save them with their original names prefixed with "extracted_".
In summary, the python-docx module offers a variety of tools for working with images in Word documents, from adding and resizing images to extracting them from existing documents.
react-intl liquid sqldatasource haskell flask-restful istio-gateway real-time-data greedy io file-storage