from openpyxl import load_workbook
wb = load_workbook("C:op.xlsx")
ws = wb.active
这是我到目前为止的代码。基本上,我想创建一个程序,其中python请求字符串并在给定的excel文件中找到该字符串。然后在另一个工作表中打印该字符串。
基本上,在excel表格中,在每一行和每个单元格中迭代,并寻找模式。然后在输出文件中每行写入一个字符串。
from openpyxl import load_workbook, Workbook
def find(wb, string):
res = []
for ws in wb:
for row in ws.values:
for value in row:
if value is not None and string in str(value):
res.append(value)
return res
if __name__ == '__main__':
wb = load_workbook("C:op.xlsx")
values = find(wb, "findme") # Replace findme with the string to find
wb = Workbook()
ws = wb.active
for value in values:
ws.append([value])
wb.save(filename="out.xlsx") # change out.xlsx with the output file name