最近想用xlwings 对excel 进行筛选 粗略翻了下 官方文档 https://docs.xlwings.org/en/stable/index.html
也没有找到相关的内容, 然后搜了下 找到了个 关于pywin32 的 帖子 https://stackoverflow.com/questions/2967949/setting-criteria-on-an-autofilter-in-pywin32
根据帖子 上的内容 尝试使用api 解决了问题
# 是否筛选 if table.sheets[1].api.AutoFilterMode == True: # 取消筛选 table.sheets[1].api.AutoFilterMode = False # 筛选 table.sheets[1].range((1,1),(i,j)).api.AutoFilter(field:=int(j), Criteria1:="=1", False) """ 上面的api 后面就是用了vba的方法 field 是需要筛选的列 criteria1 是条件 最后一个false 代表不显示下拉箭头 #############################
'vba 代码如下
Sub filter()
Dim ws As Worksheet
Dim i&, j&
Set ws = ActiveWorkbook.Worksheets(1)
MsgBox ws.AutoFilterMode
i = ws.Cells(Rows.Count, 1).End(xlUp).Row
j = ws.Cells(1, Columns.Count).End(xlToLeft).Column
Debug.Print i; j
If ws.AutoFilterMode Then
ws.AutoFilterMode = False
End If
ws.Range(ws.Cells(1, 1), ws.Cells(i, j)).AutoFilter field:=3, Criteria1:=">=5"
i = 0
For Each rngcell In [a1].CurrentRegion.SpecialCells(xlCellTypeVisible).Areas
i = i + rngcell.Rows.Count
Next rngcell
'https://blog.csdn.net/taller_2000/article/details/81269915s
'i = ws.Cells(Rows.Count, 1).End(xlUp).Row
Debug.Print "条目"; i - 1
If ws.AutoFilterMode Then
ws.AutoFilterMode = False
End If
End Sub