当前位置: 首页 > 知识库问答 >
问题:

Python-Openpyxl-在一列多页中搜索字符串值。如何在for循环中检测Null、None、False

乜承嗣
2023-03-14

搜索Excel文件字符串使用Openpyxl-(Python 3.10)

我正在搜索一个字符串,在列A中,在四个工作表中。如果在工作表中找到单元格值,则脚本可以工作。

我想知道如何确定细胞是否不存在?(这样我就可以使用一个替代字符串来搜索)。

脚本(我附上了结果的照片)

from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import Color, PatternFill, Font, Border
from openpyxl.styles import colors
from openpyxl.cell import Cell
import colorama
from colorama import init
init()
from colorama import Fore, Style, Back
import os
import shutil
import math
from decimal import Decimal
from openpyxl.styles import Alignment
from inspect import currentframe
import time

########################################
#########################################

src_path ="/Users/data/"
xlfileName = "test.xlsx"
xlfile = src_path + xlfileName
wb = Workbook()
wb = load_workbook(xlfile)

#########################################
#########################################

def get_linenumber():
    cf = currentframe()
    return cf.f_back.f_lineno

#########################################
#########################################

#########################################
#########################################

for xws in wb.sheetnames:
    worksheet = wb[xws]
    print(Style.RESET_ALL)
    print(Fore.BLUE, "Looping Through Worksheet Name -- ", worksheet, "Script line is -- ", get_linenumber())
    print(Style.RESET_ALL)
    for cell in worksheet['A']:
        search_string33 = "Current debt"  #
        search_string34 = "test"  #
        try:
            if search_string33 == cell.value:
                offset_colB_CD = cell.offset(row=0, column=1)
                print(Fore.RED, ( "Line 52 -- worksheet -- ", worksheet, "Searching for --", search_string33, "isinstance --", isinstance(search_string33, type(None))))
                if(offset_colB_CD.coordinate is None):
                    print("Line 54 -- null value detected in -- ", worksheet)
                elif(offset_colB_CD.coordinate is not None):
                    print(Fore.LIGHTRED_EX, "Line 56 -- worksheet -- ", worksheet," -- cell cordinate found --", offset_colB_CD.coordinate, " -- cell value found --", offset_colB_CD.value )
                    print(Style.RESET_ALL)
                else:
                    print("Line 59 if statement passed by on else")
                time.sleep(5)
        except (AttributeError, TypeError):
            continue


共有1个答案

裴宜春
2023-03-14

我能想到的一个快速解决方案是按照您想要搜索的顺序迭代字符串列表。

例子:

search_strings = ["Current debt", "test"]

for xws in wb.sheetnames:
    worksheet = wb[xws]
    print(Style.RESET_ALL)
    print(Fore.BLUE, "Looping Through Worksheet Name -- ", worksheet, "Script line is -- ", get_linenumber())
    print(Style.RESET_ALL)

    current_search_index = 0
    found_cell = False
    for current_string in search_strings:
        for cell in worksheet['A']:
            try:
                if current_string == cell.value:
                    offset_colB_CD = cell.offset(row=0, column=1)
                    print(Fore.RED, ( "Line 52 -- worksheet -- ", worksheet, "Searching for --", current_string, "isinstance --", isinstance(current_string, type(None))))
                    if(offset_colB_CD.coordinate is None):
                        print("Line 54 -- null value detected in -- ", worksheet)
                    elif(offset_colB_CD.coordinate is not None):
                        print(Fore.LIGHTRED_EX, "Line 56 -- worksheet -- ", worksheet," -- cell cordinate found --", offset_colB_CD.coordinate, " -- cell value found --", offset_colB_CD.value )
                        print(Style.RESET_ALL)
                        found_cell = True
                    else:
                        print("Line 59 if statement passed by on else")
                    time.sleep(5)
            except (AttributeError, TypeError):
                continue
        if found_cell == False:
            current_search_index += 1
        else:
            break
 类似资料:
  • 我的问题 当我搜索时,它会按要求返回,但当我搜索时,它会返回,为什么会出现这种情况,以及如何修复它?

  • 我有这样一个数据帧: 我想选择第二列包含单词“NL”的行,它类似于SQL命令。有人知道Python Pandas中类似的命令吗?

  • 我有一个正在解析的JSON字符串,我想检索字段“pr_num”的值。我收到这个错误: 我的代码如下: 我想获取字段的值,它们是690052和null。 jsonString如下所述

  • 问题内容: 我想要一个自动完成功能。我对数据类型的属性有简短的描述性字符串。我在redis中有一个ID列表,其中包含按创建日期排序的数据类型,并且使用ID来设置和获取该数据类型的属性,如redis类型文档中所述。我不使用哈希表。在此设置下,获取与输入到自动完成输入框中的内容相匹配的字符串集的最佳方法是什么?遍历所有id并检查我要搜索的属性- 对于每次击键,这似乎是错误的方法。 编辑: 除了下面的答

  • 问题内容: 我想在arraylist中搜索一个字符串。我的ArrayList包含: 现在我要搜索,它应该返回包含和的列表。我该如何实施? 问题答案:

  • 问题内容: 如何搜索in ,其中的字符串具有随机的列数? 我知道这是不正确的sql,但是会是这样的: 提前为SO社区 问题答案: 将全文索引添加到所有这些表中的所有字符串列,然后合并结果