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