简体   繁体   English

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

[英]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.

相关问题 如何使用 openpyxl 库将列从一张 excel 表复制到另一张 python? - How can I copy columns from one excel sheet to another with python using openpyxl Library? Excel 到 Python 字典,用于使用 Openpyxl 进行过滤 - Excel to Python Dictionary for Filtering with Openpyxl 如何在 Python 中使用 Openpyxl 对多行的 excel 行进行平均? - How to average across excel rows for multiple rows using Openpyxl in Python? 如何使用openpyxl复制excel(B2:B15)中的一系列数据? - How can I copy a range of data in excel (B2:B15) down with openpyxl? 如果我使用 openpyxl 有行号,如何迭代 excel 工作表的特定行? - how can I iterate specific rows of excel worksheet if I have row numbers using openpyxl? 如果满足条件,如何使用 openpyxl python 删除 excel 中的特定行 - How to delete specific rows in excel with openpyxl python if condition is met 为什么我不能使用python和openpyxl在excel中更改单元格值? - Why can't I change the cell value in excel using python and openpyxl? 如何在 Python 中将整列数据(行数不固定)复制到同一个 Excel 文件中的新列? - How can I copy an entire column of data (the number of rows are not fixed) to a new column in the same excel file, in Python? Python Openpyxl 根据单元格值从行中复制数据并粘贴到 ExcelSheet 的特定行中 - Python Openpyxl Copy Data From Rows Based on Cell Value& Paste In Specific Rows of ExcelSheet 如何使用 openpyxl 从 excel 文件制作 python 字典? - how can I make a python dictionary from excel file using openpyxl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM