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

Python - Openpyxl - Searching for a string value in one column, multiple sheets. How to detect for Null, None, False in a for loop

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

我正在搜索字符串,在A列中,在四个工作表。如果单元格值工作表[=36]中找到,则脚本有效 =].

我想知道如何判断如果单元格不存在? (这样我就可以使用替代字符串进行搜索)。

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

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


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

示例:

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
    if found_cell == False:
        print(Fore.RED, "Line 67 -- ", current_string, "not found in -- ", worksheet)
        print(Style.RESET_ALL)

worksheet.values 将 return 一个元组列表。在这一点上,我们可以使用列表理解和过滤来减少我们的列表。

matching_strs = ['Current_debt', 'test']
matches = [{'ws':ws, 'row_num':row_num, 'row':row} for ws in wb.worksheets for row_num, row in enumerate(ws.values) if row[0] in matching_strs]

这将 return 一个字典列表,具有 ws、row_num、行的属性。其中应提供打印结果所需的所有信息。

if 测试不需要 ( )

要打印结果只需循环和测试


for match in matches:
    # Return position [1] is the equivalent of column B
    # since indexing starts at 0.
    if match['row'][1]:
        print( Match found... )

问题的答案

在我最初的问题中,我未能查看列中直接查询的元素数。因此,如果没有达到直接命中,它就会继续前进。我 不能使用 'None',因为它有 其他元素

最好的选择是构建一个列表然后过滤列表以查看它是否包含 字符串。如果它包含字符串 invoke first choice command,如果不包含 invoke alternative command.

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)
    search_string33 = "Current debt"  #
    search_string34 = "Total current liabilities"
    cell_list = []
    for cell in worksheet['A']:
        cell_list.append(cell.value)
    number_of_elements = len(cell_list)
    print("Number of elements in the list: ", number_of_elements)
    if search_string33 in cell_list :
        print(Fore.GREEN, "Yes -- ", search_string33)
        print(Style.RESET_ALL)
    elif search_string33 not in cell_list and search_string34 in cell_list:
        print(Fore.LIGHTMAGENTA_EX, "No -- ", search_string33, Fore.GREEN, "Yes -- ", search_string34)
        print(Style.RESET_ALL)
    elif search_string33 not in cell_list and search_string34 not in cell_list:
        print(Fore.RED, "Mother of Holy Crap", search_string33)
        print(Style.RESET_ALL)