The SQL Report Template is a lightweight framework for executing SQL queries against an Oracle database and generating an Excel report with the results. Users can define SQL queries in separate .sql
files, run them using the script, and receive a structured Excel report with multiple sheets corresponding to the queries.
- Executes multiple SQL queries from a specified folder.
- Connects to an Oracle database using
lightoracle
. - Generates an Excel report with separate sheets for each SQL file.
- Automatically formats tables and includes an introduction sheet.
- Supports command-line arguments for input and output customization.
- Python 3.10+
- Oracle Instant Client
- Conda environment for dependency management
This repository is available as a GitHub template, allowing users to quickly generate a new project based on its structure.
- Go to the template repository: SQL Report Template
- Click "Use this template" (green button).
- Enter a name for your new repository and click "Create repository from template".
- Clone your new repository and follow the setup instructions.
If you prefer to start with this template manually:
git clone https://github.com/GSU-Analytics/sql-report-template.git my-new-report
cd my-new-report
rm -rf .git # Removes existing Git history
Then, initialize your new repository:
git init
Follow the installation steps to set up the environment and configure the database.
git clone https://github.com/GSU-Analytics/sql-report-template.git
cd sql-report-template
Using Conda:
conda env create -f environment.yml
conda activate sql-report-env
Using pip:
pip install -e .
- Note: This installs the package in "editable" mode. You can make any changes you want to the source code, in
/src/
, and your commandline scripts will reflect those changes.
Now, run:
sql-reporter --help
If you see something like the output below, you are ready to go:
Usage: sql-reporter [OPTIONS] COMMAND [ARGS]...
A tool for turning SQL queries into standardized Excel files. Powered by
Python under the hood. Customize the Python code to make your reports
exactly how you like them.
Run the following command to enter your credentials:
sql-reporter set-user-credentials
sql-reporter execute --sql_folder_path="./working_queries" --output_file="./report/Program_Report.xlsx"
Argument | Description | Default |
---|---|---|
--sql_folder_path |
Path to folder containing .sql files. |
./working_queries |
--output_file |
Path to save the output Excel report. | ./report/Program_Report.xlsx |
Place SQL files inside working_queries/
. Each file should contain at least one query and use --
comments as query titles.
-- Cohort One Year Retention Rate
SELECT term, COUNT(*) AS student_count FROM student_data WHERE cohort_year = 2023 GROUP BY term;
The generated Excel report will include:
- Introduction Sheet – Overview of the report.
- Query Sheets – One sheet per SQL file, formatted as tables.
├── report # Output directory (ignored in .gitignore)
├── report_config # Files for updating your report configuration
│ ├── intro_text.txt # Change this to change your introduction sheet
│ └── user_config.yaml # Store your user credentials here with set-user-credentials
├── src
│ └── sql_reporter
│ ├── report_generator
│ │ ├── __init__.py
│ │ └── query_runner.py # Executes SQL queries
│ ├── __init__.py # CLI tool source code
│ └── config_utils.py # Utilities for configuring the report
├── working_queries
│ ├── sheet1.sql # Example SQL file
│ └── sheet2.sql # Example SQL file
├── environment.yml # Conda environment setup
├── pyproject.toml # For building the package with Python
├── README.md # Documentation
└── uv.lock # For building the package with Python
- Oracle client errors? Ensure that the Oracle Instant Client is installed and
lib_dir
is correctly set inconfig.py
. - No output file generated? Check that the SQL files are correctly formatted and contain queries.
- Permission issues? Ensure the script has permission to write to the output directory.
For further assistance, consult the README.md
or submit an issue in the GitHub repository.