Search for content in Excel under specified folders using Python built-in module OS and OPENPYXL

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.