Python Forum
Open excel file using Python
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Open excel file using Python
#1
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.
Reply
#2
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)
Reply
#3
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!
Reply
#4
do you need to do another object.quit after line 25?
Reply
#5
No, I would like to have the excel open to continue working on it.
Reply
#6
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
Reply
#7
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/w...SGrid.html
Reply
#8
Great! so you think it is a problem in the libraries rather then in the code?
Reply
#9
Well, a new library would most likely be used differently, so would require changes to the code
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Data Sorting and filtering(From an Excel File) PY_ALM 0 1,057 Jan-09-2023, 08:14 PM
Last Post: PY_ALM
  Python - Merge existing cells of Excel file created with xlsxwriter manonB 0 3,730 Mar-10-2021, 02:17 PM
Last Post: manonB
  Creating more than one excel File at once malvarez1976 0 1,825 Dec-15-2020, 02:04 AM
Last Post: malvarez1976
  Convert Excel to .txt - Need each excel row to be separate .txt file cowboykevin05 2 4,812 Jan-03-2020, 06:29 PM
Last Post: Larz60+
  [split] Converting excel file to txt file unexceptionalhobby 2 4,380 Oct-16-2019, 06:34 PM
Last Post: unexceptionalhobby
  Python write result of VAR to excel file wissam1974 8 8,661 Jul-13-2019, 01:09 PM
Last Post: wissam1974
  How to add a dataframe to an existing excel file wendysling 2 28,189 May-09-2019, 07:00 PM
Last Post: wendysling
  Import Excel File that Starts with Number kiki1113 1 3,324 Dec-20-2018, 07:13 PM
Last Post: Larz60+
  CSV file dont open with code ayaz786amd 2 2,631 Nov-21-2018, 09:13 AM
Last Post: ayaz786amd
  Copy raw data in excel to another new excel file keerthiprashanth 5 3,919 Oct-20-2018, 10:13 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020