Python Forum
Reading Excel file and use a wildcard in file name and sheet name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading Excel file and use a wildcard in file name and sheet name
#1
I have a simple script that reads an excel file and captures certain columns and saves to a new Excel spreadsheet. I run this script each day and before I run it I have to update the date in the file name and sheet name to match the current report. I am trying to determine if it is possible to use a wildcard in the file name and sheet name. I have tried using the * for a wildcard like this....data = pd.read_excel(r"H:\Daily Reports\Employee_*_Report.xlsx" , sheet_name='Metrics * Employee'), but it does not seem to work. I am relatively new to using Python and I am sure it is probably something simple that I am missing.

Here is the python script I am using.

import pandas as pd

data = pd.read_excel(r"H:\Daily Reports\Employee_12202021_Report.xlsx" , sheet_name='Metrics Dec 20 Employee')

#Select only certain columns
df = pd.DataFrame(data, columns= ['Lookup','Report_Date', 'Employee_ID', 'Type', 'Location', 'Sales', 'Hours'])

#load to excel and save
df.to_excel (r'H:\Monthly Reports\Completed\Employee Metrics 12202021.xlsx', sheet_name='Summary', index=False, header=True)
Reply
#2
Is there more than one file in the directory?

to get the sheet names you could do domething like:

import pandas as pd
import os

df = pd.read_excel(f'{os.getcwd()}/weather/test.xlsx', None)

for sheet in df.keys():
    print(sheet)
Output:
Sheet1
Will list all sheets in the file.

You could use listdir() to get file names
randolphoralph likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
The directory would only contain one file, and the file would have a date in the file name. I had thought about using a similar approach to list the sheet names, but wasn't sure how to account for the sheet name on each daily file to be variable when reading the excel file/sheet.
Reply
#4
(Jan-13-2022, 07:09 PM)randolphoralph Wrote: The directory would only contain one file, and the file would have a date in the file name.
Can use glob or also pathlib has glob build in.
Example
import pandas as pd
from pathlib import Path

dest = r'.'
for path in Path(dest).rglob('output_*_result.xlsx'):
    if path.is_file():
        print(path)

df = pd.read_excel(path)
print(df)
Now will this match any number.
So in my test one file with these two names read fine.
output_123_result.xlsx
output_99999_result.xlsx
As it's one file should get away with not specify sheet name.
Reply
#5
(Jan-13-2022, 07:57 PM)snippsat Wrote:
(Jan-13-2022, 07:09 PM)randolphoralph Wrote: The directory would only contain one file, and the file would have a date in the file name.
Can use glob or also pathlib has glob build in.
Example
import pandas as pd
from pathlib import Path

dest = r'.'
for path in Path(dest).rglob('output_*_result.xlsx'):
    if path.is_file():
        print(path)

df = pd.read_excel(path)
print(df)
Now will this match any number.
So in my test one file with these two names read fine.
output_123_result.xlsx
output_99999_result.xlsx
As it's one file should get away with not specify sheet name.

I added from pathlib import Path, but get an error stating ImportError: cannot import name 'path' from 'pathlib'. Does it matter that I am working within Windows?
Reply
#6
pathlib was new in Python 3.4,what version of Python do you use?
Remember that import is case sensitive,so if i test can make your error.
>>> from pathlib import path
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ImportError: cannot import name 'path' from 'pathlib' (C:\Python310\lib\pathlib.py

# It's like this 
>>> from pathlib import Path
>>> 
randolphoralph likes this post
Reply
#7
(Jan-13-2022, 09:45 PM)snippsat Wrote: pathlib was new in Python 3.4,what version of Python do you use?
Remember that import is case sensitive,so if i test can make your error.
>>> from pathlib import path
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ImportError: cannot import name 'path' from 'pathlib' (C:\Python310\lib\pathlib.py

# It's like this 
>>> from pathlib import Path
>>> 

That is exactly what the issue was. Case sensitive. Good catch and thank you for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  If I open a file write or append, is the file loaded into RAM? Pedroski55 11 1,118 Jan-14-2026, 07:49 AM
Last Post: Pedroski55
  How to write variable in a python file then import it in another python file? tatahuft 4 2,253 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 2,250 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 2,846 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  docx file to pandas dataframe/excel iitip92 1 5,689 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 14,741 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Reading an ASCII text file and parsing data... oradba4u 2 2,898 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Python openyxl not updating Excel file MrBean12 1 3,949 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 2,383 Feb-07-2024, 12:24 PM
Last Post: Viento
  file open "file not found error" shanoger 8 17,397 Dec-14-2023, 08:03 AM
Last Post: shanoger

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020