gpt4 book ai didi

python - 如何将excel日期时间作为 float 而不是xlwings中的 'datetime.datetime'?

转载 作者:行者123 更新时间:2023-12-04 10:45:02 24 4
gpt4 key购买 nike

我的代码如下,text.xlsx 文件如下所示:
text.xlsx file screenshot .

import xlwings as xw
import datetime

app = xw.App(visible=False,add_book=False)
file_path = r'D:\test.xlsx'
wb = app.books.open(file_path)
sht = wb.sheets[0]
cell = sht.range((1, 1)).value
print(cell)

wb.close()
app.quit()
在测试文件中,'A1' 中的日期时间是 (1895, 10, 24, 23, 2, 24) ,在 excel 中存储为 -1528.96 ,但我现在不关心这个日期时间,我只想得到 excel数字-1528.96 在我的代码中,但它失败了。
打印消息是:
1895-10-24 23:02:24
预期的消息是:
-1528.96
也试过: https://stackoverflow.com/a/9574948/12525445 , 但结果是 -1527.04
非常感谢任何线索!

最佳答案

从 Excel 日期获取基础浮点数的规范方法是调用 value2属性(property)。不幸的是,我们还没有在 xlwings 中实现这一点,所以如果你在 Windows 上,你需要下拉到 pywin32:

sht.range((1, 1)).api.Value2

在此处了解有关如何处理缺失功能的更多信息: http://docs.xlwings.org/en/stable/missing_features.html
.api 在 Windows 上的语法确实是 pywin32 语法,这与官方 VBA 的处理方式几乎相同。在您的具体情况下,您可以引用 Microsoft 官方文档了解 Value2:
https://docs.microsoft.com/en-us/office/vba/api/excel.range.value2

关于python - 如何将excel日期时间作为 float 而不是xlwings中的 'datetime.datetime'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59744239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com