Home > Net >  Python - Cannot Open .docx when I wrote a new line to it
Python - Cannot Open .docx when I wrote a new line to it

Time:10-23

Just learning how to use files in python. I created a word document "mydoc.docx", opens properly before I execute the code below:

myfile = open('mydoc.docx','w')
myfile.write ("hello" '\n')
myfile.close()

After I ran the code above, the word document cannot be opened any more showing "Word experienced an error trying to open the file."

enter image description here

CodePudding user response:

That's expected. A "docx" has a very specific structure (under the covers, it's a series of zipped files) that includes the text of the document, plus formatting, metadata, change history, etc.

What you have written with your Python program is a plain text file.

CodePudding user response:

I think the issue might be that python with open() function, opens .docx files as binary or plain text file (however definetly not a .docx file), and by writing data to it, you actually corrupt it.

It would be better if you would use libraries that can proccess .docx files. For example: docx library

CodePudding user response:

myfile = open('mydoc.docx','w')

You are not opening the file in append mode. What you're doing here is clearing the file completely & then writing new text to it.
You'll also want to be using the binary mode for opening files like this.

Further, even if you do append text to the file's contents, it's very unlikely that you'll see your appended text in the file when you open it up.

A good thing to get into a habit of doing is checking file sizes when diagnosing problems like this. If you checked the file size it would be 6 or 7 bytes I imagine. docx files are much, much bigger.

  • Related