Python Docx offers a lot of features for document formatting. In this tutorial, we will create lists and nested lists in document using python. We discussed to get started with python-docx package and created basic documents in a post.

Lists in python-docx are just paragraphs with different styling and can be implemented using pargraph methods and their styling options. Lets create basic Bullet and Numbered lists

from docx import Document

document = Document()

# bullet lists
document.add_paragraph("This is a Bullet List", style='List Bullet')
document.add_paragraph("This is a Number List", style='List Number')

We can create any number of items in lists. Now lets create each lists with multiple values.

document.add_heading('Popular Deep Learning Frameworks', 0)
# add bullet list
document.add_paragraph('The following are the most popular deep learning frameworks.')
for framework in ["Tensorflow", "Pytorch", "Keras", "ONNX"]:
    document.add_paragraph(framework, style='List Bullet')

# add number list
document.add_paragraph('The following are the most popular programming languages.')
for language in ["Python", "Java", "C++", "JavaScript"]:
    document.add_paragraph(language, style='List Number')

Here it creates multiple list items for both bullet and number lists. Here is output for code above.

My alt text

Nested Lists

Nested lists can also be generated by providing nested level while defining list style. For example nested level 2, we can provide List Number 2 or List Bullet 2 for 2nd level. Check these examples for better understanding.

# Level 1 List item
document.add_paragraph("Programming Languages", style="List Bullet")
# Level 2 List item
document.add_paragraph("Python", style="List Bullet 2")
# Level 3 List item
document.add_paragraph("Python Docx", style="List Bullet 3")
document.add_paragraph("DocxTPL", style="List Bullet 3")

# Level 3 list item
document.add_paragraph("JavaScript", style="List Bullet 2")
# Level 3 List item
document.add_paragraph("NodeJS", style="List Bullet 3")
document.add_paragraph("VueJS", style="List Bullet 3")

It creates nested lists upto 3rd child with bullet lists. This can also be implemented by number lists in same way.

My alt text

List Text Style

List text styling can also be implemented the same way as it works in paragraphs. We can specify text style for all or specific text in list item. Here is example of providing text style for list items.

data = {
    "PythonDocx" : "Creating and Modifiying Microsoft Word Docx",
    "Flask" : "Micro Web framework for web and api development",
    "Tensorflow" : "Deep Learning Framework for training models",
    "Opencv" : "Processing images and defined different image operations",
}
# iterate over each text to add to document
for key, value in data.items():
    para = document.add_paragraph("", style="List Number")
    # add title text and change color and font weight
    title = para.add_run(f"{key}: ")
    title.font.color.rgb = RGBColor(0x00, 0x00, 0xFF)
    title.font.bold = True
    # add remaining text to list item
    para.add_run(value)

My alt text