使用openpyxl查找包含具有特定值的单元格的行(Python 3.6) [英] Using openpyxl to find rows that contain cell with specific value (Python 3.6)

查看:1218
本文介绍了使用openpyxl查找包含具有特定值的单元格的行(Python 3.6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对openpyxl完全陌生,因此,正如您可以想象的那样,当我尝试使用openpyxl时遇到了困难.

I am completely new to openpyxl so, as you can imagine, I am having pretyy hard times when I try to make use of it.

我有一个Excel报告,其中仅包含一张工作表(称为工作表1).我想在所有单元格中搜索包含特定字符串(在这种情况下为产品名称ABC)的单元格.

I have an excel report that contains only one sheet (called Sheet 1). I'd like to search all cells for those that contain specific string (product name ABC in this case).

然后,我要复制包含具有ABC产品名称的单元格的行中每个单元格的内容.并将每个单元格分配给一个变量.

Then I would like to copy contents of every cell in the rows that contain cell with ABC product name. And assign every cell to a variable.

为了让您更好地了解我要达到的目标,我将举一个例子:

To give you better idea of what I am trying to achieve I'll give you an example:

因此,在这种情况下,我将仅复制2、4、6行中的单元格(因为它们仅包含ABC产品).

So in this case I would only copy cells from rows: 2, 4, 6 (as only they contain ABC product).

我已经查找了类似的问题和答案,但是我不理解(以前从未使用过excel).

I have already looked up similar questions and answers to them but I don't understand them (never have used excel before).

推荐答案

使用openpyxl进行操作对您来说重要吗?如果没有的话,我建议使用熊猫.

is it important for you to use openpyxl to do this? i would suggest using pandas if not.

    import pandas as pd

    df = pd.read_excel("path_to_excel_file")
    df_abc = df[df["Products"] == "ABC"] # this will only contain 2,4,6 rows

然后:

    for row in df_abc.iterrows():
        # do what you want with the row 

这篇关于使用openpyxl查找包含具有特定值的单元格的行(Python 3.6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆