Below is a “starter-kit” that shows the whole flow in Python / pandas.
It should be easy to tweak once you confirm the folder layout, the date-column name, and which holiday calendar (if any) you care about.
0. What the script does
- Look in a folder, find the newest Excel file (a fresh one appears every day).
- Load it with pandas.
- Throw away today’s partial data (the “morning” rows).
- Walk backward through the data-set, keeping the last 13 complete calendar days for which rows actually exist.
• If a weekend or a holiday had no rows it will automatically skip past it so that you still end up with 13 real days of data.
- Write the 13-day slice to a new Excel file.
If you decide you really want to ignore weekends or holidays even when you do have rows, uncomment the relevant lines in the code.
1. Install dependencies
pip install pandas openpyxl python-dateutil pandas_market_calendars # openpyxl = Excel driver
pandas_market_calendars is only needed if you want a market/holiday calendar that pandas does not ship with by default (pandas has US Federal holidays already).
2. Python code
import os, glob, datetime as dt
import pandas as pd
from pandas.tseries.holiday import USFederalHolidayCalendar
# ------------------------------------------------------------------
# 1) Locate the newest Excel file in the download folder
# ------------------------------------------------------------------
DOWNLOAD_DIR = r"C:\Users\you\Downloads\DailyFiles" # <-- change me
FILE_GLOB = "*.xlsx" # or "*.xlsm" …
latest_file = max(glob.glob(os.path.join(DOWNLOAD_DIR, FILE_GLOB)),
key=os.path.getmtime)
print("Using file:", latest_file)
# ------------------------------------------------------------------
# 2) Read the file
# ------------------------------------------------------------------
DATE_COL = "Date" # <-- column in the Excel sheet that holds the dates
SHEET = 0 # or sheet name string if not the first sheet
df = pd.read_excel(latest_file, sheet_name=SHEET, engine="openpyxl")
# Force the date column into date dtype only (drop any hour:min:sec info)
df[DATE_COL] = pd.to_datetime(df[DATE_COL]).dt.date
# ------------------------------------------------------------------
# 3) Throw away today’s partial data
# ------------------------------------------------------------------
today = dt.date.today()
df = df[df[DATE_COL] < today] # keep rows earlier than today
# ------------------------------------------------------------------
# 4) Build list of the last 13 *complete* days that have data
# ------------------------------------------------------------------
unique_days = sorted(df[DATE_COL].unique(), reverse=True)
# >>> OPTIONAL: If you *must* exclude weekends even if data exists
# unique_days = [d for d in unique_days if d.weekday() < 5] # 0-Mon … 4-Fri
# >>> OPTIONAL: If you *must* exclude holidays
# holiday_cal = USFederalHolidayCalendar()
# holidays = holiday_cal.holidays(start=min(unique_days),
# end=max(unique_days)).date
# unique_days = [d for d in unique_days if d not in holidays]
last_13_days = unique_days[:13] # pick most recent 13 valid dates
df_13 = df[df[DATE_COL].isin(last_13_days)]
print("Retained dates:", sorted(last_13_days))
# ------------------------------------------------------------------
# 5) Save the 13-day slice
# ------------------------------------------------------------------
out_file = os.path.join(DOWNLOAD_DIR,
f"last_13_days_{today:%Y%m%d}.xlsx")
df_13.to_excel(out_file, index=False)
print("Wrote:", out_file)
3. Things you might need to adjust
- DOWNLOAD_DIR – where the files land.
- FILE_GLOB – e.g.
"Report_*.xlsx" if each file has a prefix.
- DATE_COL – exact header in the Excel sheet.
- Holiday calendar / weekend rules – comment or uncomment as shown.
4. Test drive
Run the script once.
It will print something like:
Using file: …\DailyFiles\Report_2023-11-22.xlsx
Retained dates: [datetime.date(2023, 11, 08) … datetime.date(2023, 11, 21)]
Wrote: …\DailyFiles\last_13_days_20231122.xlsx
Double-check the output Excel: it should hold 13 distinct dates, each date in full (all rows), no “today” partial data, and weekends/holidays handled as you specified.
Questions / tweaks?
• Different sheet names?
• Need CSV instead of Excel?
• Want to archive or delete originals after processing?
Let me know and we can tune the script further!