##language:zh #pragma section-numbers off ##含有章节索引导航的 ZPyUG 文章通用模板 <> ## 默许导航,请保留 <> = pyExcelerator操Excel = ##startInc == 日期问题 == {{{ vance ma reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Thu, Jul 10, 2008 at 12:01 subject [CPyUG:58681] 使用pyExcelerator来写Excel文件 }}} 因为要用Python写Excel ,从网上查到pyExcelerator这个模块可以读写Excel文件,做了个小测试程序试了试,发现在写日期时出现了小问题, 如: {{{ #!python from pyExcelerator import * import datetime wb = Workbook() ws0 = wb.add_sheet('attShifts') style = XFStyle() ws0.write(0, 1,datetime.datetime.now(), style) wb.save('reports.xls') }}} 我写入`datetime.datetime.now()`是可以的,程序正常. 如果我把,datetime.datetime.now()换成`datetime.date(2008, 6, 10)`这时就会报错 {{{ 'datetime.date' object has no attribute 'rpn'. }}} 这是为什么呢? == 解决 == {{{ Jimmy Kuu reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Thu, Jul 10, 2008 at 12:34 }}} pyExcelerator在往cell写入时在检查数据类型时没有把datetime.date类型作为日期来处理, 里面只检查了datetime.datetime和datetime.time, date被作为FormulaCell来处理,最终导致出错. 不知道是不是代码写的有问题,里面可接受的数据类型包括了,datetime.date类型,但是却没有判断. * 解决办法: 找到pyExcelerator目录下的Row.py文件,219行 {{{ elif isinstance(label, (dt.datetime, dt.time)):改成 elif isinstance(label, (dt.datetime, dt.time, dt.date)): }}} `要有信心自个儿Hacked` ~ ZoomQuiet ##endInc ---- '''反馈''' 创建 by -- ZoomQuiet [<>]