-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Lists showing up as normal paragraphs #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
John, from docx import Document
from docx.shared import Inches
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
def create_list(paragraph, list_type):
p = paragraph._p #access to xml paragraph element
pPr = p.get_or_add_pPr() #access paragraph properties
numPr = OxmlElement('w:numPr') #create number properties element
numId = OxmlElement('w:numId') #create numId element - sets bullet type
numId.set(qn('w:val'), list_type) #set list type/indentation
numPr.append(numId) #add bullet type to number properties list
pPr.append(numPr) #add number properties to paragraph
ordered = "5"
unordered = "1"
document = Document()
paragraph = document.add_paragraph("Hello", "List Paragraph")
create_list(paragraph, unordered)
paragraph = document.add_paragraph("Hello Again", "List Paragraph")
create_list(paragraph, unordered)
paragraph = document.add_paragraph("Goodbye", "List Paragraph")
create_list(paragraph, unordered)
paragraph = document.add_paragraph("Hello", "List Paragraph")
create_list(paragraph, ordered)
paragraph = document.add_paragraph("Hello Again", "List Paragraph")
create_list(paragraph, ordered)
paragraph = document.add_paragraph("Goodbye", "List Paragraph")
create_list(paragraph, ordered)
document.save("bullet list demo.docx") The list style is called "List Paragraph" and the type of bullet is set by adding the appropriate XML elements. python-docx apparently creates a default "numbering" XML document which governs the styles and indentation of list items; the list_type I put into numId is a reference to that document. YMMV, I don't know that this is the best way to do this, but it works... |
And thanks for the great work, @scanny! |
:) |
This is wonderful and works. |
Both 'ordererd' and 'unordered' give me an Ordered list in return. Any idea why this is happening? |
Switching the number ids from "1" to "6" for unordered version gave me bullet points correctly |
I am trying to insert numeric and bulleted lists into an existing Word document, however they are showing up as normal paragraphs:
This results in 2 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:
Did I miss anything for indenting or numbering? I'd also be interested in learning how to do this for an unordered list too.
The text was updated successfully, but these errors were encountered: