在两个 excel 工作表中搜索并查找值(xlrd 到 openpyxl)

Search and find values in two excel sheets(xlrd to openpyxl)

wrbk = xlrd.open_workbook("D:Book1.xlsx")
idx = 0

book_1 = xlrd.open_workbook("D:Book2.xlsx")
sh_1 = book_1.sheet_by_name('Sheet4')

i = 0
for x in range(sh_1.nrows):
    i = i + 1
    if i >= sh_1.nrows:
        break
    if sh_1.cell(i, 2).value:
        concat = sh_1.cell(i, 2).value
        for y in range(len(wrbk.sheets())):
            sht = wrbk.sheet_by_index(y)
            for j in range(sht.ncols):
                for cell in range(sht.nrows):
                    list = str(sht.cell(cell, j).value)
                    if list.__contains__(concat):
                        print(sh_1.cell(i, 2).value)

我正在使用此代码在工作簿中查找值,然后在另一个工作簿中搜索该值。
我正在使用 xlrd,到目前为止输出很好,但我不能用 xlrd 读写。
我需要建议来将此代码从 xlrd 更改为 openpyxl。

这定义了一个函数来进行搜索并使用 Regular Expression 进行 'contains' 匹配。更改打印以适合。

from openpyxl import load_workbook
import re

# open workbook 
excel_file1 = 'D:Book1.xlsx'
wb1 = load_workbook(excel_file1) # wrbk
ws1 = wb1["Sheet1"]

excel_file2 = 'D:Book2.xlsx'
wb2 = load_workbook(excel_file2) # book_1
ws2 = wb2["Sheet4"] # sh_1

# fn to search all sheets in workbook
def myfind(wb,s):
    for ws in wb.worksheets:       
        for c in range(1,ws.max_column+1):
            for r in range(1,ws.max_row+1):
                txt = ws.cell(r,c).value 
                if txt is None:
                    pass
                elif re.search(s,txt):
                    print("Found",s,txt,ws,r,c)

# scan col C
for r in range(1,ws2.max_row+1):
    s = ws2.cell(r, 3).value 
    if s is None: 
        pass
    else: 
        print(r,s)
        myfind(wb1,s)