Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reconstructing docx from xml files

Tags:

xml

zip

docx

From what I have read, docx files are zipped collections of xml file. On Windows 7, (the only OS on which I have tried this), if I save a file, say f.docx from Word, then exit Word and change the file name to f.zip, I can unzip the bundle and read the component files. But if I then remove then re-zip the f folder (without any modifications) and change the extension back to docx, I then get an error saying that "The file f.docx cannot be opened because there are problems with the contents". And when I look at the details, it says, "Microsoft Office cannot open this file because some parts are missing or invalid."

Question: Why is that? And how can the component pieces be reassembled into a valid docx file?

A similar question is asked here but the offered solution does not work. As I've noted above, I'm not altering anything in the folders, nor even opening the files. Although I cannot see why it would be of relevance, my method for rezipping the file is to use the context-menu command "Send to compressed (zipped) folder".

like image 754
user02814 Avatar asked Sep 02 '26 16:09

user02814


2 Answers

As @Pawel noted in his comment, the thing to do is ensure that the rezipping is done from the command line. In the absence of a built-in zip command in Windows 7 (I was unable to get the PowerShell solution mentioned here to work) one can use 7-zip for the recreation of the zipped archive; unzipping with Windows 7 context menu appears not to be the problem. There is something to be careful of using 7-zip. Assume that foo.docx has been renamed to foo.zip and uncompressed with the context menu to folder foo. Then, when it comes time to rezip the component files with 7-zip, do not zip the foo folder. Instead, descend into the foo folder, select the component files and folders, and the use 7-zip to zip those components into a foo.zip folder than can be renamed back to foo.docx.

like image 106
user02814 Avatar answered Sep 05 '26 16:09

user02814


What I do to modify docx, xlsx or pptx files w/o fiddling with zipping:

  1. add .zip to file name (file.docx.zip)
  2. navigate to the file I need to change e.g. /word/document.xml
  3. copy file outside of .zip folder
  4. modify the file
  5. copy file back into .zip folder (sometimes I need to delete the original file rather than overwriting it)
  6. remove .zip from file name (file.docx)
  7. done, open file in Word
like image 21
Juergen Riemer Avatar answered Sep 05 '26 15:09

Juergen Riemer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!