pythondocxpython-docx

Multiple Numbered Lists with python-docx


I need to generate lots of similar one-page content in a single docx file for personal purposes, so I have created a Python script to automate that. Some of this content is a numbered list, but unfortunately, the numbering continues from page to page too, such as:


Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

1. List item1

2. List item2

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

<--- Page break -->

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

3. List item1

4. List item2

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.


And I want it to look like this:


Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

1. List item1

2. List item2

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

<--- Page break -->

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

1. List item1

2. List item2

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.


To provide more details on how my code works, let's say I have a list of pages, where it's possible to get a list of items based on the page and that's implemented in the get_items(page) function. Having the above in mind, my code currently looks something like this (I'm excluding formatting paragraphs for simplicity):

document = Document()
pages = [...]                      # list of pages
for page in pages:
    document.add_paragraph("...")  # put some regular text to the page
    items = get_items(page)
    for item in items:
        p = document.add_paragraph(item, style='List Number 2')     # 'Line Numer 2' is for some extra indent
    document.add_paragraph("...")  # put some regular text to the page
    document.add_page_break()      # add page breaks to start from a new page
document.save("...")               # save the document

Solution

  • As I reviewed the SO comment mentioned by DaSt, it became clear that there's no direct way to add more than 3 numbered lists in a single document with separate numbering. The abstract numbering solution seems too complex, so the simplest way I see of solving this is the following:

    LEFT_INDENT = Pt(36)               # sample left indent for numbered list
    document = Document()
    pages = [...]                      # list of pages
    for page in pages:
        document.add_paragraph("...")  # put some regular text to the page
        items = get_items(page)
        for i in range(len(items)):
            p = document.add_paragraph(f"{i + 1}. {items[i]}")
            p.paragraph_format.left_indent = LEFT_INDENT
        document.add_paragraph("...")  # put some regular text to the page
        document.add_page_break()      # add page breaks to start from a new page
    document.save("...")               # save the document