简体   繁体   English

如何选择win32com.client必须在python中使用的excel版本?

[英]How to choose the version of excel which win32com.client has to use in python?

I have an excel 2007 file(*.xlsx) which is to be opened through a python script. 我有一个excel 2007文件(* .xlsx),它将通过python脚本打开。 But the problem is I have two versions of MS office (2003 and 2007) installed in my computer. 但问题是我的计算机上安装了两个版本的MS office(2003和2007)。 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. 虽然我尝试将Excel 2007作为打开xlsx文件的默认应用程序,但win32com.client正在尝试使用Excel 2003打开我的xlsx文件。此外,这将恢复Excel 2003作为默认应用程序。

Is there a way to force the win32com.client to choose Excel 2007 to open xlsx files? 有没有办法强制win32com.client选择Excel 2007来打开xlsx文件?

For Excel 2013, you can type: 对于Excel 2013,您可以键入:

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. 由于我没有安装任何其他版本,我想如果您只需用您需要的版本替换15就可以了。 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. 在进程资源管理器中只能看到一个Excel(14)实例。 Doing

excel15.Visible = True

confirms that. 证实了这一点。

It turns out that using automation for controlling both versions of Excel is impossible. 事实证明,使用自动化来控制两个版本的Excel是不可能的。 Checking the Registry, the programs ( Excel.Application.14 and 15 ) share the same CLSID. 检查注册表,程序( Excel.Application.1415 )共享相同的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). 每个CLSID只有一个LocalServer (我的计算机上有HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\Wow6432Node\\CLSID\\{00024500-0000-0000-C000-000000000046}\\LocalServer )。 This LocalServer points to the last installed version, Excel14 (trial) on my computer. 此LocalServer指向我计算机上最后安装的版本Excel14(试用版)。 This is the only object created by the Dispatch call. 这是Dispatch调用创建的唯一对象。

All this is explained here (§ "Using Automation to Control Microsoft Excel"). 所有这些都在这里解释(§“使用自动化控制Microsoft Excel”)。

There is hope 还有希望

As I said, this LocalServer points to the last installed version. 正如我所说,这个LocalServer指向最后安装的版本。 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 我会说“简单”(更改LocalServer指向的应用程序,以及Excel.Application条目的默认应用程序),但我更喜欢以下简单的解决方案
  • reinstall the 2007 application (of course, if you can). 重新安装2007应用程序(当然,如果可以的话)。 This is a poor/inelegant yet simple fix. 这是一个糟糕/不优雅但简单的修复。

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 ). 以下应该可以工作,因为我测试了它,虽然没有同时安装两个版本的Excel,而是强制Excel文件在Word(也就是WINWORD.exe )中打开。 Replace the path to whichever version of Excel you want to be used: 将路径替换为您要使用的任何版本的Excel:

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. C:\\\\Program Files\\\\Microsoft Office\\\\Office14\\\\EXCEL.EXE替换为您希望默认使用的实际EXCEL.exe的路径后保存上面的reg脚本(小心\\\\ s ),在you_name_it.reg 运行/ merge /双击它。 It will ask you for confirmation, give it an affirmative and check . 它会要求您确认,给予肯定和检查

I have not tried this, but perhaps you could launch the Excel version you want using something like 我没试过这个,但也许你可以使用类似的东西启动你想要的Excel版本

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 (或者手动启动Excel的子进程方法),然后尝试

wb = win32com.client.GetObject(file_path) 

to obtain the running instance. 获取正在运行的实例。

Have you tried to load the module via EnsureModule ? 您是否尝试通过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 你可以从lib/site-packages/win32com/client文件夹中的python makepy.py -i检索所需的所有内容
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. 你会在那里找到两个Excell版本,选择所需的版本,它将返回如何将它连接到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). 1) excel = mod.Application应该为你提供应用程序调度,但你可能无法看到它的属性(但版本应该是正确的,命令应该像往常一样工作)。

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!). 2) excel = win32com.client.Dispatch("Excel.Application")应该使用你刚刚生成的模块(但我不确定这是否会起作用并带来正确的版本!)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM