如何解决使用xlwings将数据帧写入excel文件时发生日期格式问题
我正在尝试使用python Xlwings读取Excel文件并将其写入另一个Excel文件,但是在output1.xlsx Excel文件中,如果日期值小于13,则日期和月份部分将互换。如果日期> = 13,则日期值与输入文件相同。当我检查数据框中的值时,它是正确的,并且在使用 Xlwings 将数据帧写入output1.xlsx文件时发生了问题。使用pandas.to_excel()时,会将值以正确的格式写入excel。请帮助我使用XLWINGS将数据写入数据框中以实现excel。
问题::
注意:在图片中左侧是输入数据,右侧是输出。我用红色突出显示了错误的数据,并且日期值小于13。
使用xlwings的预期输出:
代码:
import pandas as pd
import numpy as np
import xlwings as xw
df=pd.concat(pd.read_excel("input.xlsx",sheet_name=None,parse_dates=False,na_filter = False,dtype=str),ignore_index=True)
app = xw.App(visible=False)
# 1st output file using xlwings --> here the date vaues are wrong in output file output1.xlsx
book = xw.Book("output1.xlsx")
sht = book.sheets("Sheet1")
sht.range('A1').options(index=False,dates=False).value=df
book.save()
book.close()
app.quit()
# 2nd output file using pandas to_excel() -->> here the date values are correct
df.to_excel("output2.xlsx",index=False)
文件: input files link
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。