Skip to content

feature: index entries #137

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

Closed
wfb opened this issue Feb 4, 2015 · 8 comments
Closed

feature: index entries #137

wfb opened this issue Feb 4, 2015 · 8 comments
Labels

Comments

@wfb
Copy link

wfb commented Feb 4, 2015

I am creating a large (200+ pages) document using python-docx, and I need to create an index of all names in the document. I understand that Word marks index entries a using special XE (Index Entry) field that includes the marked main entry, e.g. { XE indexed-name }. Is it possible to do this using python-docx? I would be happy to create the actual index manually at the end, as long as the index entries are generated automatically.

@scanny scanny changed the title Creating index entries feature: index entries Feb 5, 2015
@scanny scanny added the field label Feb 5, 2015
@scanny
Copy link
Contributor

scanny commented Feb 5, 2015

Hi Walter, not presently unfortunately. This is part of the fields capability if I'm not mistaken and hasn't been implemented yet. I'll leave this issue open as a feature request for it.

@wfb
Copy link
Author

wfb commented Feb 7, 2015

With Steve's excellent help, I got it all working. Here is the relevant piece of code for marking index entries:

from docx.oxml import OxmlElement
from docx.oxml.ns import qn

def MarkIndexEntry(entry,paragraph):
    run = paragraph.add_run()
    r = run._r
    fldChar = OxmlElement('w:fldChar')
    fldChar.set(qn('w:fldCharType'), 'begin')
    r.append(fldChar)

    run = paragraph.add_run()
    r = run._r
    instrText = OxmlElement('w:instrText')
    instrText.set(qn('xml:space'), 'preserve')
    instrText.text = ' XE "%s" '%(entry)
    r.append(instrText)

    run = paragraph.add_run()
    r = run._r
    fldChar = OxmlElement('w:fldChar')
    fldChar.set(qn('w:fldCharType'), 'end')
    r.append(fldChar)

Thank you so much, Steve!
Walter

@wfb wfb closed this as completed Feb 7, 2015
@wfb wfb reopened this Feb 7, 2015
@scanny
Copy link
Contributor

scanny commented Feb 9, 2015

Glad to hear you got it working Walter :)

@hannesharms
Copy link

Would be great if you could explain how to use the function for images or tables for a newbie.
The followingg has no effect on my document:

document.add_picture('test.png', width=Inches(5.25))
paragraph = document.paragraphs[-1]
MarkIndexEntry("Test Caption",paragraph)

@scanny
Copy link
Contributor

scanny commented Feb 17, 2015

@hannesharms Please use the mailing list for support questions:
https://groups.google.com/forum/#!forum/python-docx.

@mlateb
Copy link

mlateb commented Jun 3, 2018

Thank you so much for sharing the code @wfb

@TerryDSydney
Copy link

I copied your code into my python program and it worked first time. Thanks very much, @wfb

@42scientific
Copy link

42scientific commented Feb 20, 2024

This discussion is very valuable, thank you!
i am wondering, how to refer in the document back to the figure that was referenced by the method described above. So, if you define

Figure 1: [some figure caption],

how can you refer back to this figure in the text, such as

"Figure 1 states that..."?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants