Search code examples
pythonpython-docx

python-docx WD_LINE_SPACING.SINGLE not work


Very simple code:

from docx.enum.text import WD_LINE_SPACING
from docx import Document

document = Document('demo.docx')
smi_line = document.add_paragraph('test line spacing')
smi_line.line_spacing_rule = WD_LINE_SPACING.SINGLE
document.save('demo.docx')

When i open demo.docx in MS Word i see that line spacing is multiple. What i doing wrong?


Solution

  • I think what you're looking for is the .line_spacing_rule property on the ParagraphFormat object:

    from docx.enum.text import WD_LINE_SPACING
    from docx import Document
    
    document = Document()
    paragraph = document.add_paragraph('test line spacing')
    paragraph.paragraph_format.line_spacing_rule = WD_LINE_SPACING.SINGLE
    document.save('demo.docx')