Python: Insert Images in Word

2023-09-11 01:47:11 Written by  support iceblue
Rate this item
(0 votes)

Images in Word documents can break up large blocks of text and make the content more visually interesting. In addition, they can also effectively illustrate complex ideas or concepts that are difficult to explain solely through text. In this article, you will learn how to programmatically add images to a Word document using Spire.Doc for Python.

Install Spire.Doc for Python

This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.

pip install Spire.Doc

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows

Insert an Image in a Word Document in Python

Spire.Doc for Python offers the Paragraph.AppendPicture() method to insert an image into a Word document. The following are the detailed steps.

  • Create a Document object.
  • Add a section using Document.AddSection() method.
  • Add two paragraphs to the section using Section.AddParagraph() method.
  • Add text to the paragraphs and set formatting.
  • Load an image and add it to a specified paragraph using Paragraph.AppendPicture() method.
  • Set width and height for the image using DocPicture.Width and DocPicture.Height properties.
  • Set a text wrapping style for the image using DocPicture.TextWrappingStyle property.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()

# Add a seciton
section = document.AddSection()

# Add a paragraph
paragraph1 = section.AddParagraph()
# Add text to the paragraph and set formatting
tr = paragraph1.AppendText("Spire.Doc for Python is a professional Word Python API specifically designed for developers to create, read, write, convert, and compare Word documents with fast and high-quality performance.")
tr.CharacterFormat.FontName = "Calibri"
tr.CharacterFormat.FontSize = 11
paragraph1.Format.LineSpacing = 18
paragraph1.Format.BeforeSpacing = 10
paragraph1.Format.AfterSpacing = 10

# Add another paragraph
paragraph2 = section.AddParagraph()
tr = paragraph2.AppendText("Spire.Doc for Python enables to perform many Word document processing tasks. It supports Word 97-2003 /2007/2010/2013/2016/2019 and it has the ability to convert them to commonly used file formats like XML, RTF, TXT, XPS, EPUB, EMF, HTML and vice versa. Furthermore, it supports to convert Word Doc/Docx to PDF using Python, Word to SVG, and Word to PostScript in high quality.")
# Add text to the paragraph and set formatting
tr.CharacterFormat.FontName = "Calibri"
tr.CharacterFormat.FontSize = 11
paragraph2.Format.LineSpacing = 18

# Add an image to the specified paragraph
picture = paragraph1.AppendPicture("Spire.Doc.jpg")

# Set image width and height
picture.Width = 100
picture.Height = 100

# Set text wrapping style for the image
picture.TextWrappingStyle = TextWrappingStyle.Square

#Save the result document
document.SaveToFile("InsertImage.docx", FileFormat.Docx)
document.Close()

Python: Insert Images in Word

Insert an Image at a Specified Location in a Word document in Python

If you wish to place the image at a specified location in the Word document, you can set its position through the DocPicture.HorizontalPosition and DocPicture.VerticalPosition properties. The following are the detailed steps.

  • Create a Document object.
  • Add a section using Document.AddSection() method.
  • Add a paragraph to the section using Section.AddParagraph() method.
  • Add text to the paragraph and set formatting.
  • Add an image to the paragraph using Paragraph.AppendPicture() method.
  • Set width and height for the image using DocPicture.Width and DocPicture.Height properties.
  • Set the horizontal position and vertical position for the image using DocPicture.HorizontalPosition and DocPicture.VerticalPosition properties.
  • Set a text wrapping style for the image using DocPicture.TextWrappingStyle property.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
doc = Document()

# Add a section
section = doc.AddSection()

# Add a paragraph to the section
paragraph = section.AddParagraph()

# Add text to the paragraph and set formatting
paragraph.AppendText("The sample demonstrates how to insert an image at a specified location in a Word document.")
paragraph.ApplyStyle(BuiltinStyle.Heading2)

# Add an image to the paragraph
picture = paragraph.AppendPicture("pic.jpg")

# Set image position
picture.HorizontalPosition = 150.0
picture.VerticalPosition = 60.0

# Set image size
picture.Width = 120.0
picture.Height = 180.0

# Set a text wrapping style for the image  (note that the position settings are not applicable when the text wrapping style is Inline)
picture.TextWrappingStyle = TextWrappingStyle.Through

# Save the result document
doc.SaveToFile("WordImage.docx", FileFormat.Docx)
doc.Close()

Python: Insert Images in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 25 April 2024 02:10