0

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!

3
  • 1
    The path ThisWorkbook.FullName probably has a special slash character in it that Python is interpreting as something else. What characters follow the slashes in the path? Commented Jan 16, 2019 at 19:37
  • @MBB70 when I run Debug.Print ThisWorkbook.FullName it outputs C:\Users\gageb\Desktop\test.xlsm Commented Jan 16, 2019 at 19:48
  • 1
    Your error is telling you that there is a problem at character 2-3...so I would suspect \U...Can you hard code the workbook path, or will it vary? If you can, try the answer I suggested. r'''C:\Users\gageb\Desktop\test.xlsm''' Commented Jan 16, 2019 at 19:51

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this would work, but this macro will be running on an Excel template, I need to know where the user has saved the file.
Try ... myWBName = "r'''" & ThisWorkbook.FullName & "'''" then RunPython ("import exceltest; exceltest.excelTest('" & myWBName & "')")
0

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

3 Comments

It would be good if you could tell me if the method I suggested worked also. I don't have xlwings installed. If the only difference between your answer and mine is slightly different quote structure, I'd like to know.
"Any help would be greatly appreciated!"
"Great appreciation" would be minimally expressed with at the very least an upvote of my comments and/or answer. It would take you less than a second to do this. I don't need a pat on the back here, but the fact is that my insight led you directly to your answer. It doesn't seem right for you to treat this community like an answer-mill, turning your back once you have what you need. I'm new too, but it's clear already to me that there is a culture of mutual respect here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.