Hos*_*yer 2 python excel datetime xlrd xlwt
我正在尝试将一个excel电子表格中的一些日期写入另一个.目前,我在excel中获得的表示并不是我想要的,例如:"40299.2501157407"
我可以将日期打印到控制台上,但是它似乎无法正确写入excel电子表格 - 数据必须是excel中的日期类型,我不能有它的文本版本.
这是读取日期的行:
date_ccr = xldate_as_tuple(sheet_ccr.cell(row_ccr_index, 9).value, book_ccr.datemode)
Run Code Online (Sandbox Code Playgroud)
这是写日期的行:
row.set_cell_date(11, datetime(*date_ccr))
Run Code Online (Sandbox Code Playgroud)
除了一些比较之外,在这两行之间没有对date_ccr做任何事情.
有任何想法吗?
您可以将浮点数直接写入电子表格并设置单元格的数字格式.设置使用格式num_format_str的的XFStyle,当你写的值对象.
https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/doc/xlwt.html#xlwt.Worksheet.write-method
以下示例写入日期01-05-2010.(还包括06:00:10的时间,但这是通过此示例中选择的格式隐藏的.)
import xlwt
# d can also be a datetime object
d = 40299.2501157407
wb = xlwt.Workbook()
sheet = wb.add_sheet('new')
style = xlwt.XFStyle()
style.num_format_str = 'DD-MM-YYYY'
sheet.write(5, 5, d, style)
wb.save('test_new.xls')
Run Code Online (Sandbox Code Playgroud)
xlwt源代码的examples文件夹中有数字格式(num_formats.py)的示例.在我的Windows机器上:C:\ Python26\Lib\site-packages\xlwt\examples
您可以阅读有关Excel如何存储日期的信息(本页第3部分):https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html
| 归档时间: |
|
| 查看次数: |
3381 次 |
| 最近记录: |