1

I have a folder "myfolder" which was created when I had unzipt an odt-file. I had delete the content.xml in the folder. Now I want to add a file called "content.xml" with data in it (here in the variable "content" is the xml-styled text). I tried this:

with zipfile.ZipFile('myfolder', mode='a', compression=zipfile.ZIP_DEFLATED) as zf:
        zf.writestr('content.xml', content)

I get an odt-file but it is damaged. when I unzip it there is only the content.xml in it. the mode parameter is 'a' so I thougt it will append the content.xml to the other files.

Can anybody help?

2
  • 'Do you think there is a better way to do that?' By what meaning of 'better'? Does your code work? Commented Nov 9, 2016 at 15:44
  • That is teh way to do it, if already have teh valid XML content in your variable. However, getting to this point may be tricky, and that is where third party modules such as PyODF bellow can help. Commented Nov 11, 2016 at 14:55

1 Answer 1

0

You can try to use the package odfpy.

you can see some info here and here

Odfpy is a library to read and write OpenDocument v. 1.2 files. The main focus has been to prevent the programmer from creating invalid documents. It has checks that raise an exception if the programmer adds an invalid element, adds an attribute unknown to the grammar, forgets to add a required attribute or adds text to an element that doesn’t allow it. It's on pipy, so you can install it with

you can install it with pip:

pip install odfpy
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.