How can I search text in file xlsm/xlsx/docx in Python

150 Views Asked by At

The following code can only search word in file .txt, can not search word in file xlsm/xlsx/docx. How can I search text in file xlsm/xlsx/docx?

import os
paths = r"C:\Test"
word = "Japanese"

for root, dirs, files in os.walk(paths):
    for name in files:
        with open(os.path.join(root, name),'r', errors='ignore') as fle:
            my_files_content = fle.read()
            if word in my_files_content:
                print (fle.name)
1

There are 1 best solutions below

1
On

you can use file.endswith('.txt', '.py', '.xml', '.js') to open all types of file. if you are still not clear with it please look into the project with below link it is doing the same things with multiple types of file

please find this project https://github.com/dpakverma789/Projects/blob/Python_Projects/File_Search.py

make sure this script is written for windows only hope it helps your goal