Accessing Excel from 2.7 Python

I’m trying to get an old script that uses Excel working on my apprentices’ machine, with a fresh Office 365 install, and (to simplify) on the code below it fails to add a reference to “Office” or “Microsoft.Office.Interop.Excel”, they don’t exist to it. The DLLs do seem to exist.

So what do I need to check the status of or install to get the COM automation working?

import clr

clr.AddReference(“Office”)
clr.AddReference(“Microsoft.Office.Interop.Excel”)

Hi @JimCarruthers,

i have an old Office Pro 2021 and use below code to check for install on Windows 10:

#! python 2
import sys
import clr

try:
    clr.AddReference("Microsoft.Office.Interop.Excel")
    import Microsoft.Office.Interop.Excel as Excel
    
    excelApp = Excel.ApplicationClass()
    print "Excel Version {}.".format(excelApp.Version)
    
except ImportError:
    print "Excel installation not found on this computer"
    sys.exit()

Which error do you get exactly when running above ? (it takes a bit on first run here if Excel was not opened on that day before)

Where and how many? Make a search for Microsoft.Office.Interop.Excel.dll below:

C:\Windows\assembly\GAC_MSIL

or just C: What happens if you add the reference to the dll directly using a file path eg:

clr.AddReferenceToFileAndPath(file)
import Microsoft.Office.Interop.Excel as Excel

_
c.

Well I guess what I actually have is “Office.Interop.Excel.V.9.0” and if I do add the reference directly to the file it appears to work…as in I don’t get an error there, but I can’t connect to the open Excel a few lines later…

Hi @JimCarruthers, so you have the old Office / Excel still installed along with 365 ?

If you search for Microsoft.Office.Interop.Excel.dll on the C:\ drive do you find something at all ?

btw. if you use _SetDotNetRuntime which runtime you’re running at ?

are you running your script from _EditPythonScript or the new Script Editor ?

_
c.

No it’s supposed to just have 365…