10

I have an excel 2007 file(*.xlsx) which is to be opened through a python script. But the problem is I have two versions of MS office (2003 and 2007) installed in my computer. Although I tried to make Excel 2007 as the default application to open xlsx files, the win32com.client is trying to open my xlsx file using Excel 2003. Also this is reverting back Excel 2003 as the default application.

Is there a way to force the win32com.client to choose Excel 2007 to open xlsx files?

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
sam
  • 101
  • 1
  • 6

4 Answers4

6

For Excel 2013, you can type:

o = win32com.client.Dispatch("Excel.Application.15")

since the program is located somewhere like:

C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE 

Since I do not have any other version installed, I guess it works if you just replace the 15 by the version you need. You can see the path of the binary that is launched by the command with eg. process explorer.

EDIT: This is impossible "Programmatically" :(

After having tried this:

excel15 = win32com.client.dynamic.Dispatch("Excel.Application.15")
excel14 = win32com.client.dynamic.Dispatch("Excel.Application.14")

which yields objects like this:

>> print excel15
<COMObject Excel.Application.15>
>> print excel14
<COMObject Excel.Application.14>

only one instance of Excel (14) is visible in process explorer. Doing

excel15.Visible = True

confirms that.

It turns out that using automation for controlling both versions of Excel is impossible. Checking the Registry, the programs (Excel.Application.14 and 15) share the same CLSID. An there is only one LocalServer per CLSID (HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Wow6432Node\CLSID\{00024500-0000-0000-C000-000000000046}\LocalServer on my computer). This LocalServer points to the last installed version, Excel14 (trial) on my computer. This is the only object created by the Dispatch call.

All this is explained here (§ "Using Automation to Control Microsoft Excel").

There is hope

As I said, this LocalServer points to the last installed version. There is hence a chance you can achieve what you want:

  • either you feel confident to change the registry by hand. I would say "easy" (change the application pointed by LocalServer, and the default application of the Excel.Application entry), but I will prefer the following simple solution
  • reinstall the 2007 application (of course, if you can). This is a poor/inelegant yet simple fix.
Raffi
  • 3,068
  • 31
  • 33
3

The following should work since I have tested it although not with two versions of Excel simultaneous installed but instead forcing Excel files to open up in Word (aka WINWORD.exe). Replace the path to whichever version of Excel you want to be used:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.xlsx]
"Content Type"="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
@="Excel.Sheet.Custom"
"PerceivedType"="document"

[HKEY_CLASSES_ROOT\.xlsx\Excel.Sheet.Custom]

[HKEY_CLASSES_ROOT\Excel.Sheet.Custom\shell\Open]
@="&Open"

[HKEY_CLASSES_ROOT\Excel.Sheet.Custom\shell\Open\command]
@="\"C:\\Program Files\\Microsoft Office\\Office14\\EXCEL.EXE\" /dde"

[HKEY_CLASSES_ROOT\Excel.Sheet.Custom\shell\Open\ddeexec]
@="[open(\"%1\")]"

[HKEY_CLASSES_ROOT\Excel.Sheet.Custom\shell\Open\ddeexec\application]
@="Excel"

[HKEY_CLASSES_ROOT\Excel.Sheet.Custom\shell\Open\ddeexec\topic]
@="system"

Save the above reg script after replacing C:\\Program Files\\Microsoft Office\\Office14\\EXCEL.EXE with the path to the actual EXCEL.exe you'd like to use as default(be careful with the \\s), in a you_name_it.reg and run/merge/double-click it. It will ask you for confirmation, give it an affirmative and check.

Bleeding Fingers
  • 6,993
  • 7
  • 46
  • 74
  • Have you tried your solution and does it work? I wouldn't want to change my settings based on assumptions. – richie Sep 19 '13 at 05:27
  • @richie rolling back the changes is as simple as exporting the current `[HKEY_CLASSES_ROOT\.xlsx]`(as backup) or just copying the value `Excel.Sheet.XX`, and deleting `[HKEY_CLASSES_ROOT\.xlsx\Excel.Sheet.Custom]`. – Bleeding Fingers Sep 19 '13 at 07:08
  • thanks @hus787 . If possible could you give step by step instructions to accomplish your method? – richie Sep 19 '13 at 07:08
2

I have not tried this, but perhaps you could launch the Excel version you want using something like

desired_excel_path = 'C:\\Program Files\\Microsoft Office\\Office14\\EXCEL.EXE'
file_path = 'C:\\myfile.xlsx'

subprocess.call([desired_excel_path , file_path])

(or whatever is the subprocess method of launching Excel manually that works) and thereafter try

wb = win32com.client.GetObject(file_path) 

to obtain the running instance.

Caleb Hattingh
  • 9,005
  • 2
  • 31
  • 44
  • I get the following error when I try the above `log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [add] to set object on [log4net.Repository.Hierarchy.Hierarchy]` – richie Sep 19 '13 at 05:24
0

Have you tried to load the module via EnsureModule?
The usage is:

from win32com.client import gencache
mod = gencache.EnsureModule('clsID', 'lcID','versionMajor', 'versionMinor')

And you can retrieve all those needed from python makepy.py -i available in the lib/site-packages/win32com/client folder
It will open a window where you can select the application you want. You'll find both Excell versions in there, select the desired one and it will return how to connect it to python.



From here you will have two options.
1) excel = mod.Application should give you the Application dispatch but you might not be able to see the attributes from it (but the version should be the right one and commands should work as usual).

2) excel = win32com.client.Dispatch("Excel.Application") should use the module you just generated (but I'm not sure if this will work and bring the right version!).

pekapa
  • 881
  • 1
  • 11
  • 25
  • I use option 1) to control another software, not Excel. It is needed for the software doesn't support the early-bound offered by the EnsureDispatch method. I haven't tried the version selection with Excel itself for I don't have two versions installed, but I can't imagine why it wouldn't work. – pekapa Sep 19 '13 at 11:54