问题
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