简体   繁体   English

如何在 Python 中导入 COM 对象命名空间/枚举?

[英]How do I import a COM object namespace/enumeration in Python?

I'm relatively new to programming/python, so I'd appreciate any help I can get.我对编程/python 比较陌生,所以我很感激我能得到的任何帮助。 I want to save an excel file as a specific format using Excel through COM.我想通过 COM 使用 Excel 将 Excel 文件保存为特定格式。 Here is the code:这是代码:

import win32com.client as win32 

def excel():
    app = 'Excel'
    x1 = win32.gencache.EnsureDispatch('%s.Application' % app)
    ss = x1.Workbooks.Add()
    sh = ss.ActiveSheet
    x1.Visible = True
    sh.Cells(1,1).Value = 'test write'
    ss.SaveAs(Filename="temp.xls", FileFormat=56)
    x1.Application.Quit()

if __name__=='__main__':
    excel()

My question is how do I specify the FileFormat if I don't explicitly know the code for it?我的问题是,如果我不明确知道它的代码,我该如何指定 FileFormat? Browsing through the documentation I find the reference at about a FileFormat object.浏览文档时,我找到了关于 FileFormat 对象的参考。 I'm clueless on how to access the XlFileFormat object and import it in a way that I can find the enumeration value for it.我对如何访问XlFileFormat 对象并以我可以找到它的枚举值的方式导入它一无所知

Thanks!谢谢!

This question is a bit stale, but for those reaching this page from Google (as I did) my solution was accessing the constants via the win32com.client.constants object instead of on the application object itself as suggested by Eric .这个问题有点陈旧,但是对于那些从 Google 访问此页面的人(就像我一样),我的解决方案是通过win32com.client.constants对象而不是Eric 建议的应用程序对象本身访问常量。 This lets you use enum constants just like in the VBE:这让您可以像在 VBE 中一样使用枚举常量:

>>> import win32com.client
>>> xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
>>> C = win32com.client.constants
>>> C.xlWorkbookNormal
-4143
>>> C.xlCSV
6
>>> C.xlErrValue
2015
>>> C.xlThemeColorAccent1
5

Also, unless you've manually run the makepy utility, the constants may not be available if initializing the application with the regular win32com.client.Dispatch(..) method, which was another issue I was having.此外,除非您手动运行makepy实用程序,否则如果使用常规win32com.client.Dispatch(..)方法初始化应用程序,常量可能不可用,这是我遇到的另一个问题。 Using win32com.client.gencache.EnsureDispatch(..) (as the questioner does) checks for and generates the Python bindings at runtime if required.如果需要,使用win32com.client.gencache.EnsureDispatch(..) (正如提问者所做的那样)在运行时检查并生成 Python 绑定。

I found this ActiveState page to be helpful.我发现这个 ActiveState 页面很有帮助。

When I used COM to access quickbooks, I could reach the constants defined under a constants member of the object.当我使用 COM 访问 quickbooks 时,我可以访问在对象的常量成员下定义的常量 The code looked something like this (you'll be intersted in the third line):代码看起来像这样(你会在第三行感兴趣):

self._session_manager.OpenConnection2("",
                                      application_name,
                                      QBFC8Lib.constants.ctLocalQBD)

I'm not sure if this will work, but try this:我不确定这是否有效,但试试这个:

import win32com.client as win32 

def excel():
    app = 'Excel'
    x1 = win32.gencache.EnsureDispatch('%s.Application' % app)
    ss = x1.Workbooks.Add()
    sh = ss.ActiveSheet
    x1.Visible = True
    sh.Cells(1,1).Value = 'test write'
    ss.SaveAs(Filename="temp.xls", FileFormat=x1.constants.xlWorkbookNormal)
    x1.Application.Quit()

if __name__=='__main__':
    excel()

Replace xlWorkbookNormal with whatever format your trying to choose in the X1FileFormat web page you posted in your question.将 xlWorkbookNormal 替换为您在问题中发布的 X1FileFormat 网页中尝试选择的任何格式。

所有文件格式常量都记录在此处

As a general rule I find it really useful to pre-record any code in the VBA IDE in Excel.作为一般规则,我发现在 Excel 中预先记录 VBA IDE 中的任何代码非常有用。 This way you can find out all the values of constants etc that you need to use within your python code.通过这种方式,您可以找出需要在 Python 代码中使用的常量等的所有值。 You can also make sure stuff will work from within a more controlled environment.您还可以确保在更可控的环境中工作。

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

相关问题 Python:如何将某个对象的命名空间导入当前命名空间? - Python: how can I import the namespace of some object to current namespace? 如何导入Python命名空间包的所有子模块? - How do I import all the submodules of a Python namespace package? Python-如何从扩展名称空间导入父包 - Python - How do I import parent package from extended namespace 如何在 Python 中实现类似 Java 的枚举? - How do I implement a Java-like enumeration in Python? 如何构造一个 python 导入语句来检查调用命名空间中是否存在模块别名? - How do I construct a python import statement to check for the presence of a module alias in the calling namespace? 如何在Python中导入外部类并从中创建对象? - How do I import an external class and make an object out of it in Python? 如何在python中获取包的干净导入命名空间? - How do a get a clean import namespace for a package in python? 如何在Python中创建名称空间包? - How do I create a namespace package in Python? Python:如何导入命名空间的一部分 - Python: How to import part of a namespace 给定一个描述object.attribute的Python字符串,如何将属性的名称空间与属性分开? - Given a Python string describing object.attribute, how do I separate the attributes's namespace from the attribute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM