简体   繁体   中英

python - Error while opening XLS file with xlrd after creating the same file with xlwt

My script creates XLS file (with some data populated in it) using xlwt module and saves it in working directory. This first part of the operation is done successfully. In the same script, next part is to read the saved XLS file and based on that file, create another XLS file populated with some values. Once first XLS is saved I get an error to read that saved XLS using xlrd as under;

ERROR: Traceback (most recent call last):
ERROR:   File "C:\Esri_SCRIPTS\GDB_FC_SourceMapping_Service_V6\GDB_FC_SourceMapping_V6_Dev.py", line 200, in <module>
ERROR:     wb=xlrd.open_workbook(CurWrokDir + '\DataSources.xls')
ERROR:   File "C:\Python26\ArcGIS10.0\lib\site-packages\xlrd\__init__.py", line 454, in open_workbook
ERROR:     bk.parse_globals()
ERROR:   File "C:\Python26\ArcGIS10.0\lib\site-packages\xlrd\__init__.py", line 1473, in parse_globals
ERROR:     self.handle_sst(data)
ERROR:   File "C:\Python26\ArcGIS10.0\lib\site-packages\xlrd\__init__.py", line 1446, in handle_sst
ERROR:     self._sharedstrings = unpack_SST_table(strlist, uniquestrings)
ERROR:   File "C:\Python26\ArcGIS10.0\lib\site-packages\xlrd\__init__.py", line 1663, in unpack_SST_table
ERROR:     nchars = local_unpack('<H', data[pos:pos+2])[0]
error: unpack requires a string argument of length 2
ERROR:
ERROR: unpack requires a string argument of length 2

The work around I found by searching on this forum is to open the XLS output saved from first part, SAVE it and close it. Run the second part of the script and it will run successfully.

What I want to achieve is to run the script all together without any error. In other words, run the script...first part will save the XLS and immediately second part will start reading the save XLS output from first part and generate second output as XLS.

Any help will be appreciated. Thanks in advance.

There is no fundamental issue with writing a file via xlwt and re-reading it via xlrd as shown in this example:

import xlwt
import xlrd

# Write an Excel file.
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet1')

worksheet.write(0, 0, 'Hello')
worksheet.write(1, 0, 12345)

workbook.save('test.xls')

# Now read it back in.
workbook = xlrd.open_workbook('test.xls')
worksheet = workbook.sheet_by_name('Sheet1')

print worksheet.row(0)[0].value
print worksheet.row(1)[0].value

Prints:

$ python reread.py
Hello
12345.0

So it is up to you to debug what is the issue in your program that is causing the error.

Given that you don't see the issue after you save the file in Excel it may well be that this is some issue in xlwt but again you need to demonstrate that or provide some way for the developers to debug it.

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