使用Python和Win32com访问Excel COM中的枚举常量 [英] Accessing enumeration constants in Excel COM using Python and win32com

查看:267
本文介绍了使用Python和Win32com访问Excel COM中的枚举常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python 2.7 win32com模块从Python加载MS Excel工作表:

I'm using python 2.7 win32com module to load an MS Excel worksheet from Python:

    book = xlApp.Workbooks.Open("myFile.xls")
    sheet = book.Sheets(1)

Range,Worksheet等的许多方法和属性都使用诸如XlDirection,XlFillWith等枚举.这些定义了常量,例如xlDown,xlUp,xlFillWithContents等.这些常量可从win32com获得,以便我可以这样做,例如:

Many methods and properties of Range, Worksheet etc use enumerations like XlDirection, XlFillWith, and so forth. These define constants such as xlDown, xlUp, xlFillWithContents, etc. Are those constants available from win32com so that I could do, for example:

    column = outputsSheet.Range("I5:I150")
    lastRow = column.End(xlInterop.xlDown)
    print "Last row:", lastRow.Row

这不起作用,因为未定义xlInterop,是否可以使用win32com访问它?通过反复试验发现诸如xlDown之类的常量的值不是很实际.

This doesn't work because xlInterop is not defined, is there a way to access it using win32com? Discovering the values of such constants as xlDown by trial and error is not very practical.

推荐答案

我是win32com的新手,所以我不知道这是否对您有帮助...尝试用以下方法启动Excel:

I'm new to win32com, so I don't know if this helps... try to start Excel with:

xlApp = win32com.client.gencache.EnsureDispatch("Excel.Application")

这应该运行makepy,并且您可以在... \ Lib \ site-packages \ win32com \ gen_py \ 00020813-0000-0000-C000-000000000046x0x1x7中找到一些生成的Python文件(最后一个文件夹可能是您的另一个名字,我不知道).检查 init .py文件,那里定义了一堆常量.

This should run makepy, and you can find some resulting Python files in ...\Lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x7 (last folder might be another name for you, I dont know). Check the init.py file, there's a bunch of constants defined there.

您可以使用以下命令访问这些常量:

you can access these constants with:

from win32com.client import constants as c

Edit2:文件夹的路径:

path to folder:

win32com.__gen_path__

这篇关于使用Python和Win32com访问Excel COM中的枚举常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆