如何按日期筛选Excel列?

kh212irz  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(170)

底线是该列包含m/dd/yyyy形式的数据且数据类型为Date,结果Excel搞砸了过滤 * 首先选择年份,例如2023,然后选择月份,例如March*,之后我们保存文件并退出,这样对于打开文件的用户来说,他已经被这样过滤了一般来说通过COM,我找到了一种按字符串值进行过滤的方法,但下面是按日期进行过滤的方法-到目前为止我还不知道
下面是常用字符串值的解决方案

import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open('file.xlsx')
worksheet = workbook.Worksheets('Sheet1')

# Find column
date_column = worksheet.Rows(1).Find("column with text data").Column

# Filter on column
worksheet.Range('A1').AutoFilter(Field=date_column, Criteria1='Hello123')

# Save and quit
workbook.Save()
excel.Application.Quit()

这就是我如何处理约会的方法

import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open('file.xlsx')
worksheet = workbook.Worksheets('Sheet1')

# Find column "Date"
date_column = worksheet.Rows(1).Find("Date").Column

# Filter on column
worksheet.Range('A1').AutoFilter(Field=date_column, Criteria1='2023')
worksheet.Range('A1').AutoFilter(Field=date_column, Criteria1='March')

# Save and Quit
workbook.Save()
excel.Application.Quit()

导入win32 com。客户端为win32

excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open('file.xlsx')
worksheet = workbook.Worksheets('Sheet1')

date_column = worksheet.Rows(1).Find("Date").Column

worksheet.Columns(date_column).AutoFilter(Operator=14, Criteria2="2023")
worksheet.Columns(date_column).AutoFilter(Operator=11, Criteria1="March")

workbook.Save()
excel.Application.Quit()
import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open('file.xlsx')
worksheet = workbook.Worksheets('Sheet1')

date_column = worksheet.Rows(1).Find("Date").Column

worksheet.Columns(date_column).NumberFormat = "mm/dd/yyyy"

worksheet.Columns(date_column).AutoFilter(Operator=14, Criteria2="2023")
worksheet.Columns(date_column).AutoFilter(Operator=11, Criteria1="March")

workbook.Save()
excel.Application.Quit()
import win32com.client as win32
import datetime

excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open('file.xlsx')
worksheet = workbook.Worksheets('Sheet1')

date_column = worksheet.Rows(1).Find("Date").Column

date_filter = datetime.datetime(2023, 1, 1).strftime('%m/%d/%Y')

worksheet.Range('A1').AutoFilter(Field=date_column, Criteria1=date_filter, Operator=7)

workbook.Save()
excel.Application.Quit()

以上所有的方法对我都没有帮助

jyztefdp

jyztefdp1#

对于“自动套用格式”函数,“字段”值是应用该字段的范围上的整数偏移量。在这种情况下,工作表。columns(date_column)只有一列。因此,Field值为1。条件必须是字符串,并且与筛选的项目完全匹配。

相关问题