简体   繁体   English

用于Select-Case(excel)的Python Win32com.Client模块中的语法是什么

[英]What is the syntax in Python Win32com.Client module for Select-Case (excel)

So I'm trying to efficiently delete rows in excel spreadsheets that meet a certain criteria. 因此,我正在尝试有效删除符合特定条件的Excel电子表格中的行。 However, I think that it would probably be faster if I could use some built in VBA functionality that would select all the rows that I want to delete, and then delete them all at once. 但是,我认为如果我可以使用一些内置的VBA功能来选择我要删除的所有行,然后立即将它们全部删除,那么它可能会更快。

The following link is the VBA equivalent of something I'd like to do using the python win32com.client module: https://stackoverflow.com/a/31390697/9453718 以下链接是VBA相当于我想使用python win32com.client模块做的事情: https ://stackoverflow.com/a/31390697/9453718

I am currently just looping through and deleting rows as I go. 我目前正在循环并删除行。 I go through all the rows in excel, and if it meets some criteria I call: Sheet.Rows(r).EntireRow.Delete() 我浏览了excel中的所有行,如果符合某些标准,我会调用:Sheet.Rows(r).EntireRow.Delete()

Any suggestions? 有什么建议么?

Edit: here's a sample of what I currently have that deletes each row one by one 编辑:这是我目前拥有的一个逐个删除每一行的示例

import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')
book = excel.Workbooks.Open("ExcelWith200000Lines.xlsm")
sheet = book.Worksheets(0) #gets first sheet

nrows = sheet.UsedRange.Row + sheet.UsedRange.Rows.Count-1 # number of rows
ncols = sheet.UsedRange.Column + sheet.UsedRange.Columns.Count-1 # num of cols

data = sheet.Range('A1:'+NumToLetter(ncols) + str(nrows)).Value #loads all the data on the sheet into the python script

RELEVANT_COL_INDEX=0
for r in range(nrows-1, -1, -1): #starts at last row and goes to first row
    if data[r][RELEVANT_COL_INDEX] == "delete_me":
        sheet.Rows(r+1).EntireRow.Delete() #deletes the row here

book.SaveAs("myOutput.xlsm")

The SELECT CASE statement is not part of the object library of Excel but is part of the VBA language. SELECT CASE语句不是Excel对象库的一部分,但它是VBA语言的一部分。 When you connect Python as COM interface to Excel using win32com , you are only connecting to Excel and its object library including its objects (workbooks, worksheets, charts, etc.) and their properties and methods. 当您使用win32com将Python作为COM接口连接到Excel时,您只能连接到Excel及其对象库,包括其对象(工作簿,工作表,图表等)及其属性和方法。

In fact, VBA does exactly what you are doing in Python: COM interface to the Excel object library. 实际上,VBA正是您在Python中所做的:与Excel对象库的COM接口。 See under Tools \\ References and find VBA is usually the first selected, referenced library. 请参阅Tools \\ References下的内容,查找VBA通常是第一个选定的引用库。 Hence it is not part of Excel. 因此它不是 Excel的一部分。 The very Select Case docs does not indicate anywhere that is an Excel method. 选择案例文档并不表示任何Excel方法。 Thus, you can use SELECT CASE in MS Access VBA, Word VBA, Outlook VBA, etc. 因此,您可以在MS Access VBA,Word VBA,Outlook VBA等中使用SELECT CASE

Therefore, use the analogous version of VBA's SELECT CASE in Python which likely would be multi-line if and elif statements. 因此,在Python中使用类似版本的VBA的SELECT CASE ,这可能是多行ifelif语句。 While, other languages like Java, PHP, and R maintain the switch method, Python does not have this. 虽然其他语言如Java,PHP和R维护switch方法,但Python没有这个。 See Replacements for switch statement in Python . 请参阅Python中的switch语句的替换

Consider below example using your linked Excel question: 请考虑以下使用链接的Excel问题的示例:

if not (xlwsh.Cells(i, 2) in [0.4, 0.045, 0.05, 0.056, 0.063, 0.071, 0.08, 0.09]
        or xlwsh.Cells(i, 2) < 0):

    xlwsh.Rows(i).EntireRow.Delete

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

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