I have to read excel file with starting file name. So I use glob which return me the file path with file name in list. Now I want to pass the file path to Openpyxl so I can read excel. How can I do that?
p= Path(folder location where excel file is saved)
filelist = [x for x in p.glob("**/*.xlsx") if x.name.startswith("PG")]
Output:
[WindowsPath('C:/Users/XXX/Desktop/TEST/2022/05-01-2022/Spectra,/PG _380806.xlsx')]
I tried to get value from list using for loop because it's window path, I am getting error windows path not iterable.
CodePudding user response:
The load_workbook() function will accept a WindowsPath as an arugment.
Try something like this:
from openpyxl import load_workbook
from pathlib import Path
p = Path('data')
filelist = [x for x in p.glob("**/*.xlsx") if x.name.startswith("PG")]
for f in filelist:
wb = load_workbook(f)
ws = wb.active
print(f"cols={ws.max_column} rows={ws.max_row}")
# do something with the worksheet