Python: Align Text in Word

2024-04-11 05:59:15 Written by  support iceblue
Rate this item
(0 votes)

Text alignment in Word refers to the way text is aligned along the horizontal axis of the page. Proper text alignment can significantly affect the overall appearance and aesthetics of a document, making it more engaging for readers. There are several types of text alignment available in Word:

  • Left Alignment: Text is aligned along the left margin.
  • Center Alignment: Text is centered between the left and right margins.
  • Right Alignment: Text is aligned along the right margin.
  • Justified Alignment: Text is aligned along both the left and right margins.
  • Distributed Alignment: Text is evenly distributed between the left and right margins.

In this article, you will learn how to set different text alignments for paragraphs in Word in Python 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

Align Text in Word in Python

With Spire.Doc for Python, you can get the paragraph formatting through the Paragraph.Format property, and then use the HorizontalAlignment property of the ParagraphFormat class to align text left/ right, center text, justify text, or distribute text in Word paragraphs. The following are the detailed steps:

  • Create a Document instance.
  • Add a section to the document using Document.AddSection() method.
  • Add a paragraph to the section using Section.AddParagraph() method, and then append text to the paragraph.
  • Get the paragraph formatting using Paragraph.Format property.
  • Set left/center/right/justified/distributed text alignment for the paragraph using ParagraphFormat.HorizontalAlignment property.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document instance
document = Document()

# Add a section
sec = document.AddSection()

# Add a paragraph and make it left-aligned
para = sec.AddParagraph()
para.AppendText("This paragraph is left-aligned.")
para.Format.HorizontalAlignment = HorizontalAlignment.Left

# Add a paragraph and make it centered
para = sec.AddParagraph()
para.AppendText("This paragraph is centered.")
para.Format.HorizontalAlignment = HorizontalAlignment.Center

# Add a paragraph and make it right-aligned
para = sec.AddParagraph()
para.AppendText("This paragraph is right-aligned.")
para.Format.HorizontalAlignment = HorizontalAlignment.Right

# Add a paragraph and make it justified
para = sec.AddParagraph()
para.AppendText("This paragraph is justified.")
para.Format.HorizontalAlignment = HorizontalAlignment.Justify

# Add a paragraph and make it distributed
para = sec.AddParagraph()
para.AppendText("This paragraph is distributed.")
para.Format.HorizontalAlignment = HorizontalAlignment.Distribute

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

Python: Align Text 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