Schedule a monthly export
Download last month's parsed data as an Excel workbook on a schedule, with cron or GitHub Actions.
Finance and operations teams often want one spreadsheet per month. This script downloads the previous calendar month's entries from the export endpoint and saves them as <parser>-YYYY-MM.xlsx.
The script
"""Download last month's SimplyParse data for one parser as an .xlsx file."""
import os
from datetime import date, timedelta
import requests
PARSER_ID = os.environ["SIMPLYPARSE_PARSER_ID"] # from app.simplyparse.com/parsers/<id>
TOKEN = os.environ["SIMPLYPARSE_API_TOKEN"]
NAME = os.environ.get("EXPORT_NAME", "export")
def last_month() -> tuple[date, date]:
first_of_this_month = date.today().replace(day=1)
last_day = first_of_this_month - timedelta(days=1)
return last_day.replace(day=1), last_day
def main() -> None:
start, end = last_month()
response = requests.get(
f"https://api.simplyparse.com/dapi/v1/parser/{PARSER_ID}/export/",
headers={"Authorization": f"Token {TOKEN}"},
params={"processed_from": start.isoformat(), "processed_to": end.isoformat()},
timeout=300,
)
if response.status_code != 200:
raise SystemExit(f"Export failed ({response.status_code}): {response.text[:300]}")
path = f"{NAME}-{start:%Y-%m}.xlsx"
with open(path, "wb") as f:
f.write(response.content)
print(f"Saved {path} ({len(response.content):,} bytes) for {start} to {end}")
if __name__ == "__main__":
main()Run it on a schedule
Run at 06:00 on the 1st of every month:
0 6 1 * * cd /opt/exports && SIMPLYPARSE_PARSER_ID=... SIMPLYPARSE_API_TOKEN=... EXPORT_NAME=supplier-invoices /usr/bin/python3 monthly_export.py >> export.log 2>&1Adapt it
- Email or upload the file after saving it, for example to S3, Google Drive or SharePoint.
- Several parsers: loop over a list of
(name, parser_id)pairs. - Weekly instead of monthly: change
last_month()to return the previous Monday–Sunday and update the schedule. - Need JSON, not Excel? Fetch entries per document instead (see Process a folder of documents), or receive them as they're processed with a webhook.