Search code examples
pythondocpython-docx

Is there a workaround for adding Microsoft Word footnotes dynamically in Python?


Currently I'm using Python Flask to build an application to generate a formatted document from data stored in sql database. My idea was to detect the footnotes marks in the text and render footnotes on the run:

text = ''' This is a sample text {{footnotes:Yes it is!}} It's just that. '''
footnote = text[text.find("{{footnotes:")+12:text.find("}}")]

However, it seems that python-docx does not offer footnotes manipulation at this very moment.Are there any other packages in Python (or node.js, preferably not for the application would be made from scratch all over again) to add footnotes to .doc file?

Is it a viable route to first make the document Markdown and then somehow convert it into .docx with footnotes properties?


Solution

  • From the python-docx documentation (https://python-docx.readthedocs.io/en/latest/user/documents.html):

    The feature set is still being built out, so you can’t add or change things like headers or footnotes yet

    However you can locate them with the xml structure as discussed at https://github.com/python-openxml/python-docx/issues/1

    Or perhaps a better solution is shown at Python: Is there a way I can add a footnote to word document?

    If I understand this solution he is suggesting to essentially create a template with your desired footnote on it, and then load that into python. This wouldn't be "dynamic" though so is likely not what you're looking for either.

    It looks like in 2014 one of the developers of python-docx had a conversation at https://groups.google.com/forum/#!topic/python-docx/4iQoFG2X03I where he says he wouldn't work on the project for a few months. Maybe you could try to contact him there or through the main docx site and help him build out the module (so you don't have to tackle all the xml handling on your own). Otherwise you will have to find another solution (you mentioned Node.js, but I have no knowledge of it so I cannot help there). Best of luck!