Under the specified path, searching for the specified content contained in the Excel file, you first need to traverse the specified path to get the absolute/relative path of all Excel files under that path, and then read the contents of Excel, judging the value of each cell in the file and the content to be searched ( Regular comparison, equivalent comparison ). Therefore, implementing this feature requires two parts of the content, path traversal , and Excel file content reading .
Use the OS module to traverse all Excel files under a specified path
import OS def files (dirpath, Suffix=[ " .xls , " " ]): for root, dirs, files in Os.walk (Dirpath ): for name in files: if Os.path.splitext (name) [-1] in suffix: yield Os.path . Join (Root, name)
Reading the contents of an Excel file using OPENPYXL
ImportOPENPYXLdefecontent (FH, query): WB=Openpyxl.load_workbook (FH) sheets=Wb.sheetnames forNameinchSheets:sheet=Wb[name] forRinchRange (1, sheet.max_row+1): forCinchRange (1, sheet.max_column+1): v= Sheet.cell (Row=r, column=c)ifv = =query:Print("The string {} was found in the {} file's Table { }". Format (FH, name, query))
Resources
Openpyxl
Os
Search for content in Excel under specified folders using Python built-in module OS and OPENPYXL