I am unable to find examples where xlwt is used to write into existing files. I have a existing xls file that I need to write to. When I use xlrd to read the file, I cant seem to figure out how to transform the "Book" type returned into a xlwt.Workbook. I would appreciate if someone can point me to an example.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
There are no examples. It is not possible. Not with xlwt, nor with any other software. The XLS file structure is complicated, and doesn't act like a database to which you can append rows in a table of your choice.
Whatever software you use has to make like a user with a copy of Excel and a keyboard: (1) "open the file" i.e. load the contents into memory (2) manipulate the in-memory information (3) "save" (which blows away the existing file and replaces it with a new file) or "save as" (which writes a new file and leaves the existing file unchanged).
I told you this about 12 hours ago but here it is again:
Visit this summary site.
Points of interest:
xlutils package
tutorial on xlrd, xlwt, and xlutils ... contains examples
google-group / mailing-list for asking questions like this (helps to have worked through the tutorial first)
I had the same problem. My customer ordered me Python 3.4 script that updates XLS (not XLSX) Excel files.
The 1st package xlrd was installed by "pip install" without problems in my Python home.
The 2nd one xlwt needed to say "pip install xlwt-future" to be compatible.
The 3rd one xlutils has no support for Python 3, but I adapted it a little bit and now it works at least for dummy script:
I attached the file here: http://ifolder.su/43507580
Write to alexander.samoylov@gmail.com if it got expired.
P.S.: Some functions are not called in the dummy example, so maybe they will need for an adaptation also. Who wants to do it, fix exceptions one-by-one with a google help. It's not a very difficult task, because the package code is small...
You need
xlutils.copy
. Try something like this:Keep in mind you can't overwrite cells by default as noted in this question.
The code example is exactly this:
You'll need to create book1.xls to test, but you get the idea.
I openpyxl
Here's some sample code I used recently to do just that.
It opens a workbook, goes down the rows, if a condition is met it writes some data in the row. Finally it saves the modified file.