Paragraph spacing and line spacing are crucial formatting options in Microsoft Word that greatly influence the visual presentation and readability of your documents. Paragraph spacing determines the vertical space between paragraphs, creating a distinct separation between each paragraph. Line spacing, on the other hand, controls the vertical distance between lines within a paragraph, directly impacting the density and readability of the text. By appropriately setting paragraph spacing and line spacing, you can easily create visually appealing and easy-to-read documents. In this article, we will explain how to set paragraph spacing and line spacing in Word documents 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
Set Paragraph Spacing in Word in Python
Spire.Doc for Python provides the Paragraph.Format.BeforeSpacing and Paragraph.Format.AfterSpacing properties to adjust the spacing before and after a paragraph. The detailed steps are as follows.
- Create an object of the Document class.
- Add a section to the document using Document.AddSection() method.
- Add two paragraphs to the section using Section.AddParagraph() methods.
- Set the spacing before and after the paragraphs using Paragraph.Format.BeforeSpacing and Paragraph.Format.AfterSpacing properties.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create an object of the Document class document = Document() # Add a section to the document section = document.AddSection() # Add two paragraphs to the section para1 = section.AddParagraph() para1.Format.HorizontalAlignment = HorizontalAlignment.Center textRange1 = para1.AppendText("Spire.Doc for Python") textRange1.CharacterFormat.TextColor = Color.get_Blue() textRange1.CharacterFormat.FontName = "Calibri" textRange1.CharacterFormat.FontSize = 15 para2 = section.AddParagraph() textRange2 = para2.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.") textRange2.CharacterFormat.FontName = "Calibri" textRange2.CharacterFormat.FontSize = 12 # Set the spacing after the first paragraph para1.Format.AfterAutoSpacing = False para1.Format.AfterSpacing = 10 # Set the spacing before and after the second paragraph para2.Format.BeforeAutoSpacing = False para2.Format.BeforeSpacing = 10 para2.Format.AfterAutoSpacing = False para2.Format.AfterSpacing = 10 # Save the result file document.SaveToFile("SetParagraphSpacing.docx", FileFormat.Docx2013) document.Close()
Set Line Spacing in Word in Python
To set the pacing between lines in a paragraph, you can use the Paragraph.Format.LineSpacing property. The detailed steps are as follows.
- Create an object of the Document class.
- Add a section to the document using Document.AddSection() method.
- Add a paragraph to the section using Section.AddParagraph() methods.
- Set the spacing between lines in the paragraph using Paragraph.Format.LineSpacing property.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create an object of the Document class document = Document() # Add a section section = document.AddSection() # Add a paragraph to the section para = section.AddParagraph() textRange = para.AppendText("Spire.Doc for Python is a proven reliable MS Word API for Python which enables to perform many Word document processing tasks. Spire.Doc for Python 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.") textRange.CharacterFormat.FontName = "Calibri" textRange.CharacterFormat.FontSize = 12 # Set line spacing rule para.Format.LineSpacingRule = LineSpacingRule.Multiple # Set line spacing value (The line spacing rule "Multiple" with value 18 sets the line spacing to "1.5 lines", value 12 sets the line spacing to "Single") para.Format.LineSpacing = 18 # Save the result file document.SaveToFile("SetLineSpacing.docx", FileFormat.Docx2013) document.Close()
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.