如何在 xlwings 中将日期解析为参数

问题描述 投票:0回答:1

我想将日期解析为 python 函数中的参数,但它无法识别。

例如:

def myfunction(cob_ToGet):
    # cob_ToGet is datetime
    print(cob_ToGet.strftime("%d.%m.%Y"))

并从 vba 调用以下内容:

RunPython ("import mypythonfile; mypythonfile.myfunction(cob_ToGet=28.09.2023)")
python-3.x xlwings
1个回答
0
投票

我会用

Sub tester()
    RunPython "import newproject; newproject.myfunction(cob_ToGet='28.09.2023')"
End Sub

或者只是

Sub tester()
    RunPython "import newproject; newproject.myfunction('28.09.2023')"
End Sub

也在你的职能中

def myfunction(cob_ToGet):
    # cob_ToGet is datetime
    print(cob_ToGet.strftime("%d.%m.%Y"))

cob_ToGet
是一个字符串,所以strftime会导致错误。

这样的操作会将值返回到单元格“A1”

import xlwings as xw
from datetime import datetime


def myfunction(cob_ToGet):
    # cob_ToGet is datetime
    wb = xw.Book.caller()
    wb.sheets['Sheet1']['A1'].value = datetime.strptime(cob_ToGet, "%d.%m.%Y")
© www.soinside.com 2019 - 2024. All rights reserved.