简体   繁体   中英

Are xlrd and xlwt compatible?

I'm trying to create a workbook with python and I need to test the values of different cells to fill it but I'm having some troubles. I use xlrd and xlwt to create and edit the excel file.

I've made a little example of my problem and I don't understand why it's not working.

import xlwt
import xlrd
wb = xlwt.Workbook()
ws = wb.add_sheet('Test')
ws.write(0,0,"ah")
cell = ws.cell(0,0) # AttributeError: 'Worksheet' object has no attribute 'cell'
print cell.value

I had taken for granted that xlrd and xlwt have shared classes which can interact with each other but it doesn't seem to be the case. How do I get the cell value of an open Worksheet object?

From my understanding, the xlwt and xlrd modules are separate and used to either read entire books into memory, or write date from memory into the file. If you're looking to use these two modules I believe you have to do something like:

1) Read an ENTIRE existing file into memory. (xlrd) 2) Edit the workbook in memory 3) Write out the workbook. (xlwt)

In other words they DON'T work interchangeably. What DOES work like this though is a different module called 'openpyxl' this module lets you create objects and manipulate them during run-time in the way you are trying to above.

Link: http://packages.python.org/openpyxl/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM