Python-docx: Support for footnote.

Created on 3 Jan 2014  Â·  12Comments  Â·  Source: python-openxml/python-docx

A footnote is two thing :

  • in the document.xml, at the place of the referecence :
<w:r>
  <w:rPr>
    <w:rStyle w:val="FootnoteReference"/>
  </w:rPr>
  <w:footnoteReference w:id="1"/>
</w:r>
  • in the footnote.xml the content of the footnote :
<w:footnote w:id="1">
  <w:p w:rsidRDefault="00D935D7" w:rsidR="00D935D7">
    <w:pPr>
      <w:pStyle w:val="FootnoteText"/>
    </w:pPr>
    <w:r>
      <w:rPr>
        <w:rStyle w:val="FootnoteReference"/>
      </w:rPr>
      <w:footnoteRef/>
    </w:r>
    <w:r>
      <w:t xml:space="preserve"> Note</w:t>
    </w:r>
  </w:p>
</w:footnote>
new parts

Most helpful comment

Four years later, and I need this :smile:
:+1: from me for this issue!

All 12 comments

@julienfr112 check out the notation I added above, makes the XML much more readable :)

It's just adding triple-backticks on a line by themselves before and after the indented code, and an optional language name, 'xml' in this case.

Good to know, thank you !

Added a first shot at (end|foot)notes support in #53

How this is going? There is an api available yet for footenotes? If not, there is any workarounds till its done?
I need a simple function that adds a footenote reference at the end of a run, and a footenote text at the bottom of the same page. Something like
run.add_footenote('Footnote text')
Thanks in advance! And great job :)

Hi @Huyston. Haven't gotten to this yet, might be a while. You might look at the work @ludoo did in #53 to see if that can work for you. There are a lot of features on the docket to add so might be a while before I get to this one.

The first challenge is to get access to the notes part, then you can parse through the XML it contains. Looks like Ludovico has solutions to those problems, so you may be able to use his as is or extend it to meet your purposes.

Four years later, and I need this :smile:
:+1: from me for this issue!

@scanny
Hey, I've started working with python-docx, and i'ts great.
I've been looking for ways to add FootNotes, and have seen the problem I'm facing is not new reading this issue.

  1. Possible to merge the Read-Only-Footnotes functionality added by ludoo? (at https://github.com/python-openxml/python-docx/pull/53#issuecomment-453834065)
  2. Adding the Write-Footnotes functionality as Huyston requested? ( at https://github.com/python-openxml/python-docx/issues/1#issuecomment-73216969)
  3. Can either you @scanny or @ludoo help me figure out how to add new FootNotes to a new/existing docx file?
    The API usage will be best, something like "run.add_footenote('Footnote text')",
    but can't, well, is there any other way to do it programmatically other than opening up the xml-file after it as been compiled as docx, and adding hardcoded-string to that file?
    An Example of usage will be very appreciated.
    Thanks so much.

@bayoog has added some low-level support for writing footnotes at https://github.com/python-openxml/python-docx/pull/624. I believe it also contains some API usage, like text.paragraph.add_footnote(text)

@scanny please merge and add support for these!!

the problem i’m facing right now: assigning text to a run removes all footnote references. any easy way to get around this? i don’t need to edit footnotes – just keep existing ones intact.

fwiw: #53 worked for me. i was able to modify Runs without losing associated references. of course, i had to modify the code a little bit because the library didn’t even support editing Run text back then.

link to my branch in case anyone needs it: https://github.com/xrisk/python-docx/tree/footnotes

I just uploaded PR to provide access to footnotes and endnotes. It might be helpful if you check my PR.
https://github.com/python-openxml/python-docx/pull/860

@xrisk You should not assign text to runs directly or it will remove all child elements a run have. Currently, you should inspect and change child elements of a run individually and prevent changing content of footnoteReference element.

Was this page helpful?
0 / 5 - 0 ratings