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

查看:812
本文介绍了使用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,您可以在...中找到一些生成的Python文件... \Lib\site-packages\win32com\gen_py \00020813-0000-0000-C000-000000000046x0x1x7(最后一个文件夹可能是你的另一个名字,我不知道)。检查 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

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

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