面对XLWT和XLRD的问题 - 同时读写 [英] Facing problem with XLWT and XLRD - Reading and writing simultaneously

查看:299
本文介绍了面对XLWT和XLRD的问题 - 同时读写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临xlrd和xlwt的问题。粘贴示例代码
以下。

 从xlwt import工作簿,公式,XFStyle 
import xlrd

book = Workbook()
sheet1 = book.add_sheet('Sheet 1')
myFontStyle = XFSt​​yle()
myFontStyle.num_format_str ='0.00'
sheet1.write(0,0,10,myFontStyle)
sheet1.write(0,1,20,myFontStyle)
sheet1.write(1,0,Formula('AVERAGE(A1:B1)' ),myFontStyle)
book.save('formula.xls')

wb = xlrd.open_workbook('formula.xls')
sh = wb.sheet_by_index(0)
for rownum in range(sh.nrows):
print sh.row_values(rownum)

想法是写一些值到Excel文件,有一些excel
具体的函数,如LogNormal,StdDev等,并使用XLRD读取计算的
值。



通过运行上面的代码,我得到以下结果是
不需要的: -

  [10.0,20.0] 
[u'','']



理想情况下,我应该在第二排有15个。当我打开Excel时,它完美地写入Excel
,但XLRD不返回结果。我非常关键的项目是
。请求您亲切的
最早回复。



感谢和问候
Tarun Pasrija

解决方案

这是昨天在python-excel google-group上给同一个问题的答案。



[背景:我是xlrd的作者/维护者,xlwt的维护者]



xlrd和xlwt都不包含公式评估引擎。这与其他免费(任何意义上)并以解释语言编写的软件包相同。本教程中记录了您可以通过 http://www.python-excel.org 下载的教程。请参见第17和36页。



如果将脚本分解成两部分,请执行第一个,使用Excel / OOo calc / Gnumeric打开结果XLS文件, [/ f]

其他可能性:



(1)在gnumeric邮件列表中查询是否可以以编程方式驱动gnumeric到以下程度:open named XLS文件,[重新]计算所有公式,保存为命名的XLS文件包含重新计算的公式结果 - 有必要强调这一点,因为上次我问,本机gnumeric文件格式不包括计算公式的结果)。



(2)您可以向Openoffice.org的calc程序提出同样的问题,或许有消息:comp.lang.python和/或www.stack overflow.com ...它有一组称为PyUNO的API;上一次我看,大多数民间放弃了试图通过和了解文件数量很多。 (3)LogNormal(我认为你的意思是LOGNORMDIST和/或LOGINV),但是我们可以在Python +电子表格世界中感受到更好的消息。



<和StdDev几乎不是Excel特定的。你可以用Python来计算自己的结果;需要增加xlwt以允许调用者为Formula单元格提供结果值,而不是插入不变的零长度字符串。



4)告诉我们你的更高层次的目标,我们可以提出其他建议。



这是非常关键的项目学术/慈善/商业企业?您是否考虑过Resolver One( http://www.resolversystems.com/products/resolver-one/ )? AFAIK,他们提供折扣,他们的产品是污垢廉价,约每100美元许可证。


I am facing a problem with xlrd and xlwt. Pasting the sample code below.

from xlwt import Workbook, Formula, XFStyle
import xlrd

book = Workbook()
sheet1 = book.add_sheet('Sheet 1')
myFontStyle = XFStyle()
myFontStyle.num_format_str = '0.00'
sheet1.write(0,0,10, myFontStyle)
sheet1.write(0,1,20, myFontStyle)
sheet1.write(1,0,Formula('AVERAGE(A1:B1)'), myFontStyle)
book.save('formula.xls')

wb = xlrd.open_workbook('formula.xls')
sh = wb.sheet_by_index(0)
for rownum in range(sh.nrows):
    print sh.row_values(rownum)

The idea is to write some values to Excel file, have some excel specific functions like LogNormal, StdDev etc and read the calculated values using XLRD.

By running the above code, I get the following results which are undesirable:-

[10.0, 20.0]
[u'', '']

Ideally I should have got 15 on the second row. It writes the Excel perfectly when I open it but XLRD does not return the results. I am stuck with this for a very critical project. Request you to kindly respond earliest.

Thanks and Regards Tarun Pasrija

解决方案

Here is the answer I gave to the same question on the python-excel google-group yesterday.

[Background: I'm the author/maintainer of xlrd and the maintainer of xlwt]

Neither xlrd nor xlwt contains a formula evaluation engine. This is in common with other packages which are free (in any sense) and written in an interpreted language. This is documented in the tutorial that you can download via http://www.python-excel.org ... see pages 17 and 36.

If you break up your script into two pieces, execute the first, open the result XLS file with Excel/OOo calc/Gnumeric, [may need to hit F9 here to recalculate], save again, execute the 2nd script piece: xlrd will display the results.

Other possibilities:

(1) Ask on the gnumeric mailing list if it is possible to drive gnumeric programmatically to the extent of: open named XLS file, [re-]calculate all formulas, save as named XLS file (with the recalculated formula results included -- it is necessary to stress this because the last time I asked, the native gnumeric file format did NOT included the calculated formula results).

(2) You could ask the same question about Openoffice.org's calc program, perhaps on news:comp.lang.python and/or www.stackoverflow.com ... it has a set of APIs called "PyUNO"; last time I looked, most folk gave up trying to wade through and understand the humungous quantity of documentation. Any better news would I'm sure be gratefully received in the Python+spreadsheet world.

(3) "LogNormal" (I presume you mean LOGNORMDIST and/or LOGINV) and "StdDev" are scarcely Excel-specific. You could calculate your own results in Python; xlwt would need to be augmented to allow the caller to supply a result value for a Formula cell, instead of plugging in an invariant zero-length string as you noticed.

(4) Tell us your higher-level objectives ... we may be then able to come up with other suggestions.

Is this "very critical project" academic / for a charity / for a commercial enterprise? Have you considered Resolver One (http://www.resolversystems.com/products/resolver-one/) ? AFAIK, they offer discounts and their product is dirt-cheap anyway at about USD 100 per licence.

这篇关于面对XLWT和XLRD的问题 - 同时读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆