Python Forum
Open excel file using Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Open excel file using Python (/thread-4133.html)



Open excel file using Python - cioer23 - Jul-25-2017

HI all! I am new in Python coding and I have a problem in the below:

object = win32com.client.Dispatch('Excel.Application')
object.visible=1
object.Run("Disability_Run")
print("Macro ran successfully!")
wbName=object.ActiveWorkbook.Name
absPath=os.path.abspath(wbName)

print("absPath=",absPath)
print("Active WB:", wbName)

for wb in object.Workbooks:
   print("WB:",wb.Name)
   wb.Save()
   #The loop above saves all the workbooks that are open

object.quit()

#Open the Excel file again

object=win32com.client.Dispatch('Excel.Application')
object.visible=1
wb1=object.Workbooks.open('C:\\Program Files\\SunGard\Prophet Excel Reporting\\Altsta_14b\\ExcelRPM.xlam')
wb2=object.Workbooks.open('C:\\Program Files\\SunGard\Prophet Excel Reporting\\Altsta_14b\\Perms.xlam')
wb3=object.Workbooks.open('C:\\Program Files\\SunGard\Prophet Excel Reporting\\Altsta_14b\\ProphetQueries.xlam')
object.RegisterXLL('C:\\Program Files\\SunGard\\Prophet Excel Reporting\\x86\\GleanExcelResults.xll')
#The above makes not receive the error message about the 'GleanReleaseComponentLocks' macro, because it imports the Prophet Add-in. However still not receive the prophet add in

wb=object.Workbooks.open(absPath)
What I am trying to do is:

1) Run the "Disability_Run" macro from the Active excel WB that I have already open;

2) Defining the WB name and its location in a variable called absPath;

3) Close the open Excel WB;

4) Open the Excel WB again trying to use the absPath variable instead of typing down the whole WB name and location.

The code works fine until the last line:

wb=object.Workbooks.open(absPath)
Even though I do not receive any error message I am not able to open again the file with this line once has been closed.

It is very important for me not making any reference to the name of the WB as the name of the file will change every quarter.

Thanks in advance for your help.


RE: Open excel file using Python - Larz60+ - Jul-25-2017

Quote:Even though I do not receive any error message I am not able to open again the file with this line once has been closed.
How do you know the file's not open?
Even though you're not getting an error, there must be some sort of indication.
also, when you do object.quit, does that actually close the file, or just quit the win32com process, leaving the file open.
do you need to do another object.quit after line 25?

finally, which packages are you using (your imports)


RE: Open excel file using Python - cioer23 - Jul-25-2017

FIrst of all thanks for your response!
The code opens the excel application, but not the exact excel file that I need to use (and that i was using to run the below code).
The problem is exactly that I do not receive any indication! It looks like the code worked but it did not!
The object.quit is used in my code to close all the excel applications, as, the purpose of the code, is closing everything and opening that again in order to keep the excel memory very low.
The python version is 3.6.1 :)

Thank you!


RE: Open excel file using Python - Larz60+ - Jul-25-2017

do you need to do another object.quit after line 25?


RE: Open excel file using Python - cioer23 - Jul-26-2017

No, I would like to have the excel open to continue working on it.


RE: Open excel file using Python - snippsat - Jul-26-2017

Just to say,win32 is a old way to work with Excel files,that very few use anymore because of better tools..
Python Excel Tutorial: The Definitive Guide
Using Notebook Jupyter the dataframe also look like Excel.
Common Excel Tasks Demonstrated in Pandas


RE: Open excel file using Python - Larz60+ - Jul-26-2017

There's now also wx.lib.agw.xlsgrid.XLSGrid available in wxpython
Here's a screenshot of that:
see: https://wxpython.org/Phoenix/docs/html/wx.lib.agw.xlsgrid.XLSGrid.html


RE: Open excel file using Python - cioer23 - Jul-26-2017

Great! so you think it is a problem in the libraries rather then in the code?


RE: Open excel file using Python - Larz60+ - Jul-26-2017

Well, a new library would most likely be used differently, so would require changes to the code