[英]How can I copy Excel rows, filtering for a value, with Python and openpyxl?
I have a gigantic excel workbook with a lot of personal data. 我有一本巨大的Excel工作簿,其中包含许多个人数据。 Each person has a unique numeric identifier, but has multiple rows of information. 每个人都有唯一的数字标识符,但具有多行信息。
I want to filter all the content through that identifier, and then copy the resulting rows to a template excel workbook and save the results. 我想通过该标识符过滤所有内容,然后将结果行复制到模板excel工作簿中并保存结果。 I'm trying to do this with Python and openpyxl. 我正在尝试使用Python和openpyxl做到这一点。
I thought that applying an AutoFilter and then copying the results would solve the problem. 我认为应用自动筛选然后复制结果将解决问题。 But it seems that openpyxl can only apply the AutoFilter and not do the actual filtering? 但是似乎openpyxl只能应用自动筛选,而不能进行实际筛选?
I tried to follow the answer to this question , but it won't do anything. 我试图遵循该问题的答案,但是它不会做任何事情。 I want to filter the number in column D (4). 我想过滤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!')
Any insight on this will be appreciated. 任何对此的见解将不胜感激。 Thanks! 谢谢!
EDIT: corrected column number according to @alexis suggestion, although it has not solved the issue. 编辑:根据@alexis的建议更正了列号,尽管它尚未解决问题。
SOLVED: it turns out that the IF statement asks for an integer, not a string. 求助:事实证明,IF语句要求输入整数,而不是字符串。 Using int() solved the problem. 使用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]))
The iter_rows()
method returns a sequence of tuples, so they're indexed from zero: column D
is at index 3. In other words, try it this way: iter_rows()
方法返回一个元组序列,因此它们从零开始索引:列D
在索引3处。换句话说,尝试这种方式:
for row in ws.iter_rows():
if row[3].value == str(filterNumber):
...
If it doesn't work, have your script print some of the column's values and take it from there. 如果它不起作用,请让您的脚本打印一些列的值,然后从那里获取。 Maybe this cell's format is not what you expect, etc. 也许此单元格的格式不是您所期望的,等等。
I finally solved it! 我终于解决了! It turns out, that 原来,
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])
Asks for an integer and not a string in the IF statement. 在IF语句中要求整数而不是字符串。 Using the int() method solved the problem. 使用int()方法解决了该问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.