如何使用 Python 和 openpyxl 复制 Excel 行,过滤值?

How can I copy Excel rows, filtering for a value, with Python and openpyxl?

我有一个巨大的 excel 工作簿,其中包含大量个人数据。每个人都有一个唯一的数字标识符,但有多行信息。

我想通过该标识符筛选所有内容,然后将结果行复制到模板 excel 工作簿并保存结果。我正在尝试使用 Python 和 openpyxl.

我认为应用自动筛选然后复制结果可以解决问题。但似乎openpyxl只能应用AutoFilter和not do the actual filtering?

我尝试按照 this question 的答案进行操作,但没有任何效果。我想过滤 D 列 (4) 中的数字。

import openpyxl, os
from openpyxl.utils import range_boundaries

#Intitializes workbooks
print('Opening data file...')
min_col, min_row, max_col, max_row = range_boundaries("A:AG")
wb = openpyxl.load_workbook('Data.xlsx')
ws = wb.active
template = openpyxl.load_workbook('Template.xlsx')
templatews = template.active

#Asks for numeric identifier
print('Done! Now introduce identifier:')
filterNumber = input()

#Does the actual thing
for row in ws.iter_rows():
    if row[3].value == str(filterNumber):
        templatews.append((cell.value for cell in row[min_col-1:max_col]))

#Saves the results
template.save('templatesave.xlsx')
print('All done! Have fun!')

如有任何见解,我们将不胜感激。谢谢!

编辑:根据@alexis 的建议更正了列号,但并未解决问题。

已解决:原来 IF 语句要求的是一个整数,而不是一个字符串。使用 int() 解决了问题。

for row in ws.iter_rows():
    if row[3].value == int(filterNumber):
        templatews.append((cell.value for cell in row[min_col-1:max_col]))

iter_rows() 方法 returns 一个元组序列,因此它们是从零开始索引的:列 D 位于索引 3。换句话说,可以这样尝试:

for row in ws.iter_rows():
    if row[3].value == str(filterNumber):
        ...

如果它不起作用,请让您的脚本打印列的一些值并从那里获取。也许这个单元格的格式不是你所期望的等等

我终于解决了! 事实证明,

for row in ws.iter_rows():
    if row[1].value == int(filterNumber):
        templatews.append(cell.value for cell in row[min_col-1:max_col])

要求在 IF 语句中输入整数而不是字符串。使用 int() 方法解决了问题。