Search code examples
pythonpython-docx

python-docx - Lists showing up as normal paragraphs


I am trying to insert numeric and bulleted lists into an existing Word document, however they are showing up as normal paragraphs:

# Open up existing document
document = Document('existing_document.docx')

# Add style from example document
temp_doc = Document()
document.styles.add_style('List Number', temp_doc.styles['List Number'].type)

# Add bullet points from example document
p = document.add_paragraph()
p.style = 'List Number'
p.add_run('Item 1')
p = document.add_paragraph()
p.style = 'List Number'
p.add_run('Item 2')

# Save
document.save('new_doc_with_list.docx')

This results in 3 paragraphs. No indenting or numbering.

I have also tried to set up all the attributes the same, and then complete the runs, however this does not work either:

document.styles['List Number'].base_style = temp_doc.styles['List Number'].base_style
document.styles['List Number'].next_paragraph_style = temp_doc.styles['List Number'].next_paragraph_style
document.styles['List Number'].paragraph_format.alignment = temp_doc.styles['List Number'].paragraph_format.alignment
document.styles['List Number'].paragraph_format.first_line_indent = temp_doc.styles['List Number'].paragraph_format.first_line_indent
document.styles['List Number'].paragraph_format.keep_together = temp_doc.styles['List Number'].paragraph_format.keep_together
document.styles['List Number'].paragraph_format.keep_with_next = temp_doc.styles['List Number'].paragraph_format.keep_with_next
document.styles['List Number'].paragraph_format.left_indent = temp_doc.styles['List Number'].paragraph_format.left_indent
document.styles['List Number'].paragraph_format.line_spacing = temp_doc.styles['List Number'].paragraph_format.line_spacing
document.styles['List Number'].paragraph_format.line_spacing_rule = temp_doc.styles['List Number'].paragraph_format.line_spacing_rule
document.styles['List Number'].paragraph_format.page_break_before = temp_doc.styles['List Number'].paragraph_format.page_break_before
document.styles['List Number'].paragraph_format.right_indent = temp_doc.styles['List Number'].paragraph_format.right_indent
document.styles['List Number'].paragraph_format.space_after = temp_doc.styles['List Number'].paragraph_format.space_after
document.styles['List Number'].paragraph_format.space_before = temp_doc.styles['List Number'].paragraph_format.space_before
document.styles['List Number'].paragraph_format.widow_control = temp_doc.styles['List Number'].paragraph_format.widow_control
document.styles['List Number'].priority = temp_doc.styles['List Number'].priority
document.styles['List Number'].locked = temp_doc.styles['List Number'].locked
document.styles['List Number'].font.all_caps = temp_doc.styles['List Number'].font.all_caps
document.styles['List Number'].font.bold = temp_doc.styles['List Number'].font.bold
document.styles['List Number'].font.complex_script = temp_doc.styles['List Number'].font.complex_script
document.styles['List Number'].font.cs_bold = temp_doc.styles['List Number'].font.cs_bold
document.styles['List Number'].font.cs_italic = temp_doc.styles['List Number'].font.cs_italic
document.styles['List Number'].font.double_strike = temp_doc.styles['List Number'].font.double_strike
document.styles['List Number'].font.emboss = temp_doc.styles['List Number'].font.emboss
document.styles['List Number'].font.hidden = temp_doc.styles['List Number'].font.hidden
document.styles['List Number'].font.imprint = temp_doc.styles['List Number'].font.imprint
document.styles['List Number'].font.italic = temp_doc.styles['List Number'].font.italic
document.styles['List Number'].font.math = temp_doc.styles['List Number'].font.math
document.styles['List Number'].font.name = temp_doc.styles['List Number'].font.name
document.styles['List Number'].font.no_proof = temp_doc.styles['List Number'].font.no_proof
document.styles['List Number'].font.outline = temp_doc.styles['List Number'].font.outline
document.styles['List Number'].font.rtl = temp_doc.styles['List Number'].font.rtl
document.styles['List Number'].font.shadow = temp_doc.styles['List Number'].font.shadow
document.styles['List Number'].font.size = temp_doc.styles['List Number'].font.size
document.styles['List Number'].font.small_caps = temp_doc.styles['List Number'].font.small_caps
document.styles['List Number'].font.snap_to_grid = temp_doc.styles['List Number'].font.snap_to_grid
document.styles['List Number'].font.spec_vanish = temp_doc.styles['List Number'].font.spec_vanish
document.styles['List Number'].font.strike = temp_doc.styles['List Number'].font.strike
document.styles['List Number'].font.subscript = temp_doc.styles['List Number'].font.subscript
document.styles['List Number'].font.superscript = temp_doc.styles['List Number'].font.superscript
document.styles['List Number'].font.underline = temp_doc.styles['List Number'].font.underline
document.styles['List Number'].font.web_hidden = temp_doc.styles['List Number'].font.web_hidden
document.styles['List Number'].quick_style = temp_doc.styles['List Number'].quick_style
document.styles['List Number'].style_id = temp_doc.styles['List Number'].style_id

Did I miss anything for indenting or numbering? I'd also be interested in learning how to do this for an unordered list too.


Solution

  • Bulleted and Numbered lists in Word are more complicated than other styles. They need to be linked to a numbering format, which is quite complex in itself, and not yet supported at the API level in python-docx.

    If you can use a "template" document as a starting point that has the list styles you need already defined, that will work, but of course that's not suitable for every use case.