Hello all,
It's been a while since my last post. Things have been busy especially in Australian payroll legislation space - Payday Super, AU Legislative changes after budget, etc. etc. 📜
As I have got some breathing space for now, so here is an attempt from my end to share knowledge around how i have been using Python 🐍 to fetch data from on premise Oracle Database and then capturing the same in outlook email message.
This can be a very useful thing for anyone who want to send a summary email to his/her team or management. The design of my solution is quite generic and the good thing is, you can use the template and can add queries as per your requirement and the output will just get appended in the outlook mail body
I have made this into a two-part blog post. In this first part i'll be writing about the building blocks and components. The next part will have details with a real example
- Setup logging and directories
- Load database credentials
- Open the Oracle connection
- Build the list of SQL query definitions
- Execute each SQL query and convert results to HTML
- Assemble one HTML email body from all sections
- Create an Outlook draft with that HTML content
- cx_Oracle for the database connection to on‑prem Oracle. This is the bridge from Python into the Oracle client stack installed on your machine. It uses the username, password, and DSN values you provide to open a normal Oracle session, so anything you can query in TOAD or SQL Developer you can query from this script.
- pandas for shaping query results and converting them to HTML tables
- win32com.client for automating Outlook via COM
- logging, pathlib, dataclasses, datetime, etc. from the standard library for structure, diagnostics and configuration

- BASE_DIR is the folder containing the script file.
- CREDENTIAL_DIR = BASE_DIR / "credentials"
- LOG_DIR = BASE_DIR / "logs"
- CREDENTIAL_FILE = CREDENTIAL_DIR / "db_credentials.txt"
- name: section title displayed in the email
- description: short description under the heading
- sql: the actual SQL text to run
- empty_message: what to show if the query returns no rows


No comments:
Post a Comment