-
Notifications
You must be signed in to change notification settings - Fork 1.2k
how to create nested list? #122
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
You can achieve this using styles such as List Number, List Number 2, etc. If the built-in ones don't suit you can customize your own. The problem arises when you want to reset numbering, which is not yet supported in python-docx. Some folks have done it, but it requires a fair amount of manual work directly on the XML. |
See if this helps: mavwolverine@7cb692a numId till 10 is for predefined numbering formats. so ilvl=0, numId=10 could be your first level and first numbering series. |
I notice this issue is still open so I was wondering if someone could clarify how this should be done? @scanny you mentioned this can be done with the built-in styles, but I'm not seeing how. There's nothing in the documentation which describes how to create a nested numbered list either. If this feature relies on the change that @virajkanwade made above, can that be merged in? |
Try doing it by hand first, using Word.
If the document looks like the one you want when you're done, just apply these styles to the list paragraphs in your document according to your needs. |
Heres a snippet from my html to docx convertor. HTH.
|
@scanny I'm not sure what list menu items inside Word map to those "List Number" styles. At least the version of Word I have has a separate selection for "multi level lists" which allow me to just select all 5 of the paragraphs, apply that style, and then indent as needed and the numbering follows. I've tried many combinations with python-docx to build a doc using ListNumber, ListNumber2 etc. styles and while the indenting is correct, the numbering restarts rather than using 1.1, 1.2 etc. |
@virajkanwade Thanks for the snippet, but am I correct in saying the patch you linked earlier isn't in the base version so I'd have to manually modify it to get access to the numbering editing ability? |
@courthold That @scanny will have to confirm. Haven't had much time to work or even look at python-docx. That reminds me, I need to check if I am using my fork or the original one in my code. Thanks :) |
@virajkanwade I gave your branch a go with If I then highlight that list and choose this highlighted item from the list menu in word: .. then the list appears with correct numbering, as below: If I compare the before and after
This doesn't seem to be added by python-docx when defining the lists, so it seems like this kind of multilevel list that I'm after isn't supported at the moment, unless I'm doing something quite wrong? Thanks |
Yes, now you've jogged my memory. Doing these is a real pain in Word. Basically you have to define a new numbering definition or something, I don't remember all the details. This SO answer has some of the details from when it was fresher in my mind. But the basic gist is you need to add a new list "instance" that refers to a list definition that specifies the layout details. Crazy, I know :) You definitely want to stick to using styles for these rather than the way it's done using the toolbar icons. I'd see if you can make it work that way using the Word UI, like use list number 2 style and restart numbering at the right spots, then observe the differences in the XML. |
@scanny Thanks for following up. Just to clarify, my problem is that it is restarting the numbering in the lower level lists, I don't want it to :) If I use ListNumber, ListNumber2 etc. then each list item just restarts the count at 1 (first block below). If I keep the same formatting and indent myself, then it keeps the top level numbering order, but never uses decimals (i.e. I would like that 2nd list to go 2, 2.1, 2.1.1 ). This doesn't seem possible just with the styles that I can see. |
@Substr Did you come up with a way to handle the multi-outline list in the end? I have similar issue, not getting the 1.1, 1.2, 1.3 etc to adhere to the multi-level list definition. However, if in the generated document I change the style from List Number 2 to Normal (or anything else) and back to List Number 2 it formats as defined in the multilevel list (as desired). I'm digging in to it now, but thought I'd ask since your request is not too old. |
@junctionapps Unfortunately not. In the end we just had to ditch using correct list formatting entirely, and just manually keep track of the doc structure, left indenting paragraphs based on their 'level' and add the number to the heading of each section. |
Is there a solution to this? #these functions seem to be no longer available
p.set_numbering(0, 10)
p.set_numId(10)
p.set_ilvl(0)
p.numId(10)
p._set_ilvl(0) |
Found it.
|
Thanks @solarjoe I found that very useful. I created a template for every new document created to draw from and inherit a certain style. |
For future reference some links to the doc and the default styles: http://python-docx.readthedocs.io/en/latest/ http://python-docx.readthedocs.io/en/latest/user/styles-understanding.html#built-in-styles |
Can anyone gives an sample code to achieve below:
|
This seems like a basic question, but I cant for the life of me figure out how to cleanly indent elements of a list. Using 'List Bullet' and 'List Bullet 2' as per @solarjoe's solution, I get something similar to the attached.It appears to add extraneous linebreaks, which makes things look very messy. |
@croots, the styles are defined by your Word template. They might look different on your computer than on others. Can you re-define them? |
I'm inclined to think @solarjoe is quite right. Try adding paragraphs to the document and setting their style to List Bullet and List Bullet 2 and see what they look like. If they look the same as paragraphs added with those styles by |
how to read bullets or numbering in an existing document |
I worked through the issue of getting a list created today. I was able to use the built in features (I.e. "List Bullet", "List Bullet 2", etc..). However, after hours of googling and attempting to edit the styles I was not able to get the spacing, indention, or the symbols I wanted to use as bullets. So I built a work around class. I am not a programmer, so this can definitely be improved and forgive me for any mistakes, but it seems to work.
|
@rdt0086 Do you have an idea how can I insert this item list to a specific insertion point? |
@kf9031 I am not sure where you are starting from rather from data or from an existing document. From data I read the data with python and identify the insertion point(character location) by knowing what information it should follow and then insert using that knowledge. From a file, I would read the file as data and then do the same. You can always re-write the file with the new information/layout. The below function is where I am getting data from a pdf document by knowing the Text (they are labels) before the information I want to extract. You can use the same method to insert. This is the best example I have with me now, but perhaps later if you are still having issues I can put a more appropriate example together.
|
@rdt0086 Thanks for your quick reply. I want to insert the item list in a Word document (template) at an insertion point indicated by a label. I was hoping that there is a simple solution like this: https://stackoverflow.com/questions/24965042/python-docx-insertion-point/34814241#34814241 |
exemple:
link: ooxml numbering
The text was updated successfully, but these errors were encountered: