0

I am a complete beginner with Python (3.7). I want to manipulate Excel files and VBA. I am using Spyder 3.3.6 to write and compile the code.

I can create new blank excel sheet and perform VBA operations from scratch, however, when I try to open an existing blank .xlsm file, I receive an error.

A block of code like this works fine:

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.visible=True
xl1=xl.Workbooks.Add()
xls=xl1.Worksheets.Add()

But this following block of code does not work, specifically the last line:

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.visible=True
xl.Workbooks.Open(Filename="C:\Full\Location\test.xlsm")

The console poops out all this (I have no idea what it means):

runfile('C:/Users/xxxx/Desktop/xxfolderxx/PythonScripts/xxfilenamexx.py', wdir='C:/Users/xxxx/Desktop/xxfolderxx/PythonScripts')
Traceback (most recent call last):

  File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-17-c4b027276514>", line 1, in <module>
    runfile('C:/Users/xxxx/Desktop/xxfolderxx/PythonScripts/xxfilenamexx.py', wdir='C:/Users/xxxx/Desktop/xxfolderxx/PythonScripts')

  File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\xxxx\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/xxxx/Desktop/xxfolderxx/PythonScripts/xxfilenamexx.py", line 16
    xl.Workbooks.Open(Filename="C:\Users\xxxx\Desktop\xxfolderxx\PythonScripts\test.xlsm")
                               ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape


I also tried the same two blocks of code in a Jupyter notebook. Same results, same 'SyntaxError:'

EDIT: samutamm directed me to the solution, ty

  • Maybe same solution as here https://stackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in-my-file-path ? – samu Jan 16 '20 at 14:57
  • 2
    @samutamm yep, that solved it. I should have just popped the Syntax error line into the search bar -.- Tyvm, I have earned the Beginner stamp on my forehead – romanex1212 Jan 16 '20 at 15:04
  • Does this answer your question? [Why do I get a SyntaxError for a Unicode escape in my file path?](https://stackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in-my-file-path) – Mast Jan 16 '20 at 15:08
  • Don't mind the auto comment, that's just an indication to get this closed as a duplicate so others find the answer more easily. – Mast Jan 16 '20 at 15:08

0 Answers0