0

I am running a code as below in python to open an excel and run a macro. Basically my python script is sitting in

C:\Users\adrlee\Desktop\Python files\Automation

and my excel VBA file (Automation.xlsb) is sitting in

C:\Users\adrlee\Desktop\Python files\Automation\Powerpoint

I am running this code

fileDir = os.path.dirname(os.path.realpath('__file__'));

filename = os.path.join(fileDir, '../Powerpoint/Automation.xlsb')
filename = os.path.abspath(os.path.realpath(filename))
print(filename);

if os.path.exists("Powerpoint/Automation.xlsb"):
    xl=win32com.client.Dispatch("Excel.Application")
    xl.Workbooks.Open(filename)
    xl.Application.Quit() # Comment this out if your excel script closes
    del xl

print("Powerpoint generated");

but i am getting error

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Sorry, we couldn't find C:\\Users\\adrlee\\Desktop\\Python files\\Powerpoint\\Automation.xlsb. Is it possible it was moved, renamed or deleted?", 'xlmain11.chm', 0, -2146827284), None)

What am i doing wrong

6
  • did you try running code by giving full path manually, just to check it its working? Commented Jul 19, 2017 at 7:41
  • it only worked when I did a forward slash of the absolute path. However, this cant be as this file maybe deployed onto other's computer. Hence I was looking at the relative path Commented Jul 19, 2017 at 8:12
  • hi @YowE3K can i ask what do u mean state full path in open statement? isnt the issue here that i cant pass the excel file into the workbooks.open ()? Commented Jul 19, 2017 at 9:04
  • If your current directory is C:\Users\adrlee\Desktop\Python files\Automation and the Excel file is C:\Users\adrlee\Desktop\Python files\Automation\Powerpoint\Automation.xlsb wouldn't you want './Powerpoint/Automation.xlsb' instead of '../Powerpoint/Automation.xlsb'? Commented Jul 19, 2017 at 9:06
  • 1
    Sorry about my earlier (deleted) comment - I thought you were passing the relative path to the Open, but you are actually passing the absolute path. I think you just need to specify the path relative to the current path instead of to the current path's parent - i.e. . instead of .. Commented Jul 19, 2017 at 9:06

2 Answers 2

1

Thanks for the comments and hints guys! I managed to finally get it right:

fileDir = os.path.dirname(os.path.realpath('__file__'));

filename = os.path.join(fileDir, './Powerpoint/Funnel Automation.xlsb')
print(filename);


xl=win32com.client.Dispatch('Excel.Application')
xl.Workbooks.Open(Filename = filename)
del xl

print("Powerpoint generated");
Sign up to request clarification or add additional context in comments.

Comments

0

If fileDir contains

C:\Users\adrlee\Desktop\Python files\Automation\

then joining ..\Powerpoint\Automation.xlsb to it will give

C:\Users\adrlee\Desktop\Python files\Automation\..\Powerpoint\Automation.xlsb

which is equivalent to

C:\Users\adrlee\Desktop\Python files\Powerpoint\Automation.xlsb

because .. is equivalent to the parent directory, and the parent directory of ...\Python files\Automation is ...\Python files.


Your question states that the Excel file is actually

C:\Users\adrlee\Desktop\Python files\Automation\Powerpoint\Automation.xlsb

so you should really be joining .\Powerpoint\Automation.xlsb to the fileDir variable. (While .. refers to the parent directory, . refers to the existing directory.)

i.e. use:

filename = os.path.join(fileDir, './Powerpoint/Automation.xlsb')

1 Comment

hihi , I edited my code such that fileDir = os.path.dirname(os.path.realpath('file')); filename = os.path.join(fileDir, './Powerpoint/Automation.xlsb') filename = os.path.abspath(os.path.realpath(filename)) print(filename); xl=win32.client.Dispatch('Excel.Application') xl.Workbooks.Open(Filename = filename) del xl but im still getting the same error: 'Exception occurred.', (0, 'Microsoft Excel', "Sorry, we couldn't find C:\\Users\\adrlee\\Desktop\\Python files\\ Automation\\Powerpoint\\Automation.xlsb.

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.