I’m trying to produce numbered lists with multiple levels using Python-Docx, but I’m running into issues with sub-level numbering after uploading to Google Docs. When I open the document in Microsoft Word, the numbering looks fine; however, Google Docs fails to show the correct numeric values for nested items. I attempted to adjust the XML settings for the list, but nothing worked as expected.
Here’s a simplified example to illustrate my approach:
def build_list(doc):
doc.add_paragraph('Primary Item 1', style='List Number')
doc.add_paragraph('Nested Item 1.1', style='List Number')
doc.add_paragraph('Nested Item 1.2', style='List Number')
doc.add_paragraph('Primary Item 2', style='List Number')
from docx import Document
my_doc = Document()
build_list(my_doc)
my_doc.save('nested_list.docx')
Does anyone have advice on how to properly configure nested lists so that they maintain their numbering in both Word and Google Docs?
hey, have u tried using custom styles for each level? I had similar issues and found that defining specific styles for primary and nested items helped. also, adjusting the indentation and spacing in the style definitions can make a big difference. might be worth a shot!
I’ve encountered similar issues with nested lists in Python-Docx. One approach that worked for me was to create custom paragraph styles for each list level. Here’s a snippet that might help:
from docx.enum.style import WD_STYLE_TYPE
from docx.shared import Pt
def create_list_style(doc, name, level, indent):
style = doc.styles.add_style(name, WD_STYLE_TYPE.PARAGRAPH)
style.paragraph_format.left_indent = Pt(indent * level)
style.paragraph_format.first_line_indent = Pt(-18)
return style
# Create styles
level1 = create_list_style(doc, 'List1', 1, 18)
level2 = create_list_style(doc, 'List2', 2, 18)
# Apply styles
doc.add_paragraph('Primary Item 1', style='List1')
doc.add_paragraph('Nested Item 1.1', style='List2')
This approach gives more control over indentation and formatting. It’s not perfect, but it tends to translate better across platforms. You might need to fine-tune the indentation values for your specific needs.
I’ve dealt with this issue before, and it can be frustrating. One approach that worked for me was to use a combination of custom styles and manual numbering. Here’s what I did:
Created custom paragraph styles for each list level, similar to what DancingButterfly suggested.
Instead of relying on automatic numbering, I manually added the numbers as text. This ensures consistency across platforms.
Used tab stops to control indentation, which seems to translate better between Word and Google Docs.
Here’s a rough example of how I implemented this:
def add_list_item(doc, text, level, number):
style_name = f'ListLevel{level}'
if style_name not in doc.styles:
style = doc.styles.add_style(style_name, WD_STYLE_TYPE.PARAGRAPH)
style.paragraph_format.left_indent = Pt(18 * level)
style.paragraph_format.first_line_indent = Pt(-18)
p = doc.add_paragraph(style=style_name)
p.add_run(f'{number}. {text}')
# Usage
add_list_item(doc, 'Primary Item 1', 1, '1')
add_list_item(doc, 'Nested Item 1.1', 2, '1.1')
This approach isn’t perfect, but it gave me more control and better consistency across platforms. You might need to tweak the indentation and formatting to suit your specific needs.