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

发布于 2024-08-08 22:30:45 字数 714 浏览 4 评论 0原文

我对编程/Python 比较陌生,所以我很感激我能得到的任何帮助。我想通过 COM 使用 Excel 将 Excel 文件保存为特定格式。这是代码:

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()

我的问题是,如果我不明确知道 FileFormat 的代码,如何指定它?浏览文档,我找到了有关 FileFormat 对象的参考。我对如何访问 XlFileFormat 对象一无所知 并以我可以找到它的枚举值的方式导入它。

谢谢!

I'm relatively new to programming/python, so I'd appreciate any help I can get. I want to save an excel file as a specific format using Excel through COM. 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? Browsing through the documentation I find the reference at about a FileFormat object. 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.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

女中豪杰 2024-08-15 22:30:45

这个问题有点陈旧,但对于那些从 Google 访问此页面的人(就像我一样),我的解决方案是通过 win32com.client.constants 对象而不是应用程序对象本身访问常量 按照埃里克的建议 。这使您可以像在 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

此外,除非您手动运行 makepy 实用程序,否则如果使用常规 win32com.client 初始化应用程序,这些常量可能不可用.Dispatch(..) 方法,这是我遇到的另一个问题。使用 win32com.client.gencache.EnsureDispatch(..) (如提问者所做的那样)在运行时检查并生成 Python 绑定(如果需要)。

我发现 此 ActiveState 页面乐于助人。

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. This lets you use enum constants just like in the 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. Using win32com.client.gencache.EnsureDispatch(..) (as the questioner does) checks for and generates the Python bindings at runtime if required.

I found this ActiveState page to be helpful.

小情绪 2024-08-15 22:30:45

当我使用 COM 访问 Quickbooks 时,我可以访问对象的 constants 成员下定义的常量。代码看起来像这样(您将对第三行感兴趣):

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

我不确定这是否有效,但请尝试以下操作:

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()

将 xlWorkbookNormal 替换为您在您发布的 X1FileFormat 网页中尝试选择的任何格式问题。

When I used COM to access quickbooks, I could reach the constants defined under a constants member of the object. 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.

情痴 2024-08-15 22:30:45

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

All of the file format constants are documented here

书信已泛黄 2024-08-15 22:30:45

作为一般规则,我发现在 Excel 中预先记录 VBA IDE 中的任何代码非常有用。这样你就可以找到你需要在 python 代码中使用的常量等的所有值。您还可以确保在更受控的环境中工作。

As a general rule I find it really useful to pre-record any code in the VBA IDE in Excel. This way you can find out all the values of constants etc that you need to use within your python code. You can also make sure stuff will work from within a more controlled environment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文