-1

How can I create a word (.docx) document if not found using python and write in it?

I certainly cannot do either of the following:

file = open(file_name, 'r')
file = open(file_name, 'w')

or, to create or append if found:

f = open(file_name, 'a+')

Also I cannot find any related info in python-docx documentation at:

https://python-docx.readthedocs.io/en/latest/

NOTE:

I need to create an automated report via python with text and pie charts, graphs etc.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
sudoCoder
  • 115
  • 2
  • 13

2 Answers2

2

Probably the safest way to open (and truncate) a new file for writing is using 'xb' mode. 'x' will raise a FileExistsError if the file is already there. 'b' is necessary because a word document is fundamentally a binary file: it's a zip archive with XML and other files inside it. You can't compress and decompress a zip file if you convert bytes through character encoding.

Document.save accepts streams, so you can pass in a file object opened like that to save your document.

Your work-flow could be something like this:

doc = docx.Document(...)
...
# Make your document
...
with open('outfile.docx', 'xb') as f:
    doc.save(f)

It's a good idea to use with blocks instead of raw open to ensure the file gets closed properly even in case of an error.

In the same way that you can't simply write to a Word file directly, you can't append to it either. The way to "append" is to open the file, load the Document object, and then write it back, overwriting the original content. Since the word file is a zip archive, it's very likely that appended text won't even be at the end of the XML file it's in, much less the whole docx file:

doc = docx.Document('file_to_append.docx')
...
# Modify the contents of doc
...
doc.save('file_to_append.docx')

Keep in mind that the python-docx library may not support loading some elements, which may end up being permanently discarded when you save the file this way.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0

Looks like I found an answer:

The important point here was to create a new file, if not found, or otherwise edit the already present file.

import os
from docx import Document 

#checking if file already present and creating it if not present
if not os.path.isfile(r"file_path"):

    #Creating a  blank document
    document = Document()

    #saving the blank document
    document.save('file_name.docx')

#------------editing the file_name.docx now------------------------

#opening the existing document
document = Document('file_name.docx')

#editing it
document.add_heading("hello world" , 0)

#saving document in the end
document.save('file_name.docx')

Further edits/suggestions are welcome.

sudoCoder
  • 115
  • 2
  • 13