passing argument in xlwings RunPython VBA function

浪子不回头ぞ 提交于 2021-01-29 07:42:22

问题


I am getting some data from excel sheets using python. I'm using xlwings to accomplish this. I am trying to pass the path to the current workbook to my file, where it can use it to find the Excel book. I cannot use xlwings UDF's.

Here's my VBA code:

Sub Button1_Click()

    RunPython ("import exceltest; exceltest.excelTest('" & ThisWorkbook.FullName & "')")

End Sub

Here's my exceltest.py Python code:

def excelTest(path_of_file):
    print (path_of_file)

Here's the error I'm getting:


Error

  File "<string>", line 1

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Any help would be greatly appreciated!


回答1:


Use the full path of your workbook (the actual path instead of ThisWorkbook.FullName and structure it like this r'''myfullpath\mysubfolder\myExcelfile'''




回答2:


I needed to use RunPython ("import exceltest; exceltest.excelTest(r'" & ThisWorkbook.FullName & "')")



来源:https://stackoverflow.com/questions/54223734/passing-argument-in-xlwings-runpython-vba-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!