Last modified: Jul 08, 2026
Python-docx Line Spacing Guide
Controlling spacing in Word documents is essential for readability. When you automate document creation with Python-docx, you need precise control over line spacing and paragraph spacing. This guide explains everything clearly.
Spacing affects how your document looks. Too little spacing makes text cramped. Too much spacing wastes space. Python-docx gives you full control over both line spacing and paragraph spacing.
Understanding Line Spacing
Line spacing controls the vertical distance between lines within a paragraph. In Python-docx, you set line spacing using the paragraph_format.line_spacing property.
There are three ways to specify line spacing: single, multiple, and exact. Single spacing is 1.0. Double spacing is 2.0. Multiple spacing uses a float value like 1.5.
from docx import Document
doc = Document()
p = doc.add_paragraph('This is single spaced text.')
p.paragraph_format.line_spacing = 1.0
p2 = doc.add_paragraph('This is double spaced text.')
p2.paragraph_format.line_spacing = 2.0
p3 = doc.add_paragraph('This is one and a half spacing.')
p3.paragraph_format.line_spacing = 1.5
doc.save('line_spacing_example.docx')
You can also use exact line spacing in points. This gives you precise control. Use Pt(24) for 24 points spacing.
from docx import Document
from docx.shared import Pt
doc = Document()
p = doc.add_paragraph('This uses exact 24pt line spacing.')
p.paragraph_format.line_spacing = Pt(24)
doc.save('exact_line_spacing.docx')
Understanding Paragraph Spacing
Paragraph spacing controls the space before and after a paragraph. This is different from line spacing. Use paragraph_format.space_before and paragraph_format.space_after.
Spacing before adds space above the paragraph. Spacing after adds space below. Both are measured in points or Pt values.
from docx import Document
from docx.shared import Pt
doc = Document()
p1 = doc.add_paragraph('First paragraph.')
p1.paragraph_format.space_after = Pt(12)
p2 = doc.add_paragraph('Second paragraph with 12pt space above.')
p2.paragraph_format.space_before = Pt(12)
doc.save('paragraph_spacing.docx')
You can combine both. This creates clear visual separation between paragraphs. It is better than using blank lines.
Setting Spacing for Multiple Paragraphs
You can apply spacing to many paragraphs at once. Loop through all paragraphs in the document. This is useful for consistent formatting.
from docx import Document
from docx.shared import Pt
doc = Document()
for i in range(5):
doc.add_paragraph(f'Paragraph number {i+1}')
for paragraph in doc.paragraphs:
paragraph.paragraph_format.space_after = Pt(6)
paragraph.paragraph_format.line_spacing = 1.15
doc.save('uniform_spacing.docx')
Using Spacing with Styles
You can define spacing in styles. This applies spacing to all paragraphs using that style. It is efficient for large documents.
from docx import Document
from docx.shared import Pt
doc = Document()
style = doc.styles['Normal']
style.paragraph_format.space_after = Pt(8)
style.paragraph_format.line_spacing = 1.15
p = doc.add_paragraph('This paragraph uses the Normal style spacing.')
doc.save('style_spacing.docx')
Common Pitfalls
One common mistake is confusing line spacing with paragraph spacing. They are different. Line spacing affects lines within a paragraph. Paragraph spacing affects space between paragraphs.
Another mistake is using too large spacing values. This makes documents look unprofessional. Use small values like 6 to 12 points for paragraph spacing.
Avoid mixing units. Always use Pt() for points. Do not mix with inches or centimeters. This causes unexpected results.
Best Practices for Clean Documents
Use consistent spacing throughout your document. Set it once in a style. Apply that style to all paragraphs. This follows Python docx Best Practices for Clean Generation.
Use line spacing of 1.15 to 1.5 for body text. This improves readability. Use single spacing for lists and code blocks. Use double spacing only for drafts.
Set paragraph spacing before and after. Use 6 to 12 points after paragraphs. Use 0 to 6 points before headings. This creates clear document structure.
Advanced Spacing Control
You can also control spacing for specific paragraph elements. For example, headings often need more space before and less after.
from docx import Document
from docx.shared import Pt
doc = Document()
heading = doc.add_heading('Section Title', level=1)
heading.paragraph_format.space_before = Pt(24)
heading.paragraph_format.space_after = Pt(12)
body = doc.add_paragraph('This is body text after the heading.')
body.paragraph_format.space_after = Pt(6)
doc.save('heading_spacing.docx')
You can combine spacing with other formatting. Use bold or italic for emphasis. But keep spacing consistent.
Testing Your Spacing
Always test your spacing by opening the generated document. Visual inspection is the best way to catch errors. Adjust values as needed.
Use the paragraph_format.line_spacing_rule to check the current rule. This helps debug issues.
from docx import Document
from docx.enum.text import WD_LINE_SPACING
doc = Document()
p = doc.add_paragraph('Test paragraph.')
rule = p.paragraph_format.line_spacing_rule
print(f'Line spacing rule: {rule}')
p.paragraph_format.line_spacing = 2.0
rule = p.paragraph_format.line_spacing_rule
print(f'After change: {rule}')
Line spacing rule: SINGLE
After change: MULTIPLE
Conclusion
Mastering line spacing and paragraph spacing in Python-docx is key to professional documents. Use line_spacing for lines within a paragraph. Use space_before and space_after for gaps between paragraphs. Apply spacing through styles for consistency. Test your output visually. With these techniques, you can create clean, readable Word documents programmatically. For more advanced layout control, check out Python docx Multi-Column Layouts Guide and Python DOCX Pagination Control Guide.