简体   繁体   English

python-使用xlwt创建相同文件后,使用xlrd打开XLS文件时出错

[英]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. 我的脚本使用xlwt模块创建XLS文件(其中填充了一些数据)并将其保存在工作目录中。 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. 在同一脚本中,下一部分是读取保存的XLS文件,并基于该文件创建另一个填充了一些值的XLS文件。 Once first XLS is saved I get an error to read that saved XLS using xlrd as under; 保存了第一个XLS后,出现如下错误,无法使用xlrd读取已保存的XLS;

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. 通过在该论坛上搜索发现的解决方法是,打开从第一部分保存的XLS输出,然后保存并关闭。 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. 换句话说,运行脚本...第一部分将保存XLS,第二部分将立即开始从第一部分读取保存的XLS输出并生成第二输出为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: 如下例所示,通过xlrd写入文件并通过xlwt重新读取文件没有根本问题:

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. 鉴于在Excel中保存文件后看不到问题,很可能这是xlwt问题,但是再次需要证明这一点或为开发人员提供调试方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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