Exporting SQL queries and/or Pandas DataFrames to Excel, with support for chart generation and data visualization.
Project description
A Python library for exporting SQL queries and/or Pandas DataFrames to Excel, with support for chart generation and data visualization.
SQL2Excel
SQL2Excel exports result sets from SQL queries and/or Pandas DataFrames into Excel including chart generation and data visualization with a strong focus on automating and simplifying the process of exporting data from SQL/Python to Excel. It supports various chart types and allows for basic chart customization.
Installation
- Create a virtual environment (
.env)
python -m venv .env
- Activate the environment
source .env/bin/activate
- Install
SQL2Exceland its dependencies
pip install SQL2Excel
- Verify the Installation
python -c "import sql2excel; print(f'version: {sql2excel.__version__}')";
!!! tip
To install SQL2Excel dependencies for a specific database (e.g. Postgres), see Database Support.
Getting Started
SQL2Excel requires you to add special comments (called directives) to your SQL script to instruct it how to export the query result.
-- my_script.sql
-- chart
SELECT year, revenue FROM annual_sales;
-- chart=bar
SELECT region, SUM(revenue) AS total_revenue
FROM regional_sales
GROUP BY region
ORDER BY total_revenue DESC;
-- exec
CREATE TEMPORARY TABLE tmp_table AS ...
-- This query will be SKIPPED
SELECT * from foo.bar
-
-- chartdirective will export the query result to Excel without generating any chart because the chart type is not provided. -
-- char=bardirective will export the query result to Excel and generate a bar chart from the result. -
-- execexecutes the third query without exporting any result. This is useful for DDL, DML, and temporary tables that might be used in subsequent query. -
The fourth query will be ignored because it does not contain any directives.
Then, run the command sql2excel in your terminal:
sql2excel my_script.sql \
--dialect postgresql \
--host localhost \
--port 5432 \
--user username \
--password secret \
--dbname my_db \
--output report.xlsx
That's it!
Story of SQL2Excel
SQL2Excel stems from a project at CREST to profile the scientific performance of the African countries across 52 scientific disciplines using the Web of Science database. This resulted in generating 1560 reports, each with several performance indicators and different visualizations. SQL2Excel made such job possible!
Why Use SQL2Excel
The main reasons why you should consider using SQL2Excel despite the existence of Openpyxl are:
-
Integration with SQL
SQL2Excelexecutes your SQL script and exports all results to Excel including chart generation.SQL2Exceloffers support for exporting data from parameterized queries. This improves the efficiency of repetitive tasks.
-
Support for Pandas DataFrames
SQL2Excelexports DataFrames to Excel. WhilePandassupports exporting dataframe to Excel,SQL2Exceloffers more than just writing the dataframe to Excel. SQL2Excel can generate a full report from different dataframes including different visualizations.
-
Matplotliband externally generated images- Inserting
Matplotlibfigures and externally generated images is straightforward inSQL2Excel. This is useful if you require advanced charts that cannot be generated in Excel.
- Inserting
-
Simple API
SQL2Exceloffers simple API for data export and chart generation.
SQL2Excel is not meant as a library for reading/writing into Excel and it is not suitable for fine customizations of Excel. Its purpose is to improve productivity, automate repetitive tasks, and simplify data export and chart creation.
If you want more control over excel from within Python, use Openpyxl instead.
Database Support
SQL2Excel works with any database supported by SQLAlchemy. This includes PostgreSQL, MySQL, Oracle, and SQL Server. It is currently tested against PostgreSQL and MySQL.
PostgreSQL
To install SQL2Excelfor postgres you have two options:
- Using
pyscopg2-binary
pip install SQL2Excel[postgres]
- Using pycopg3
pip install SQL2Excel[postgres-psycopg]
MySQL
pip install SQL2Excel[mysql]
Oracle
pip install SQL2Excel[oracle]
SQL Server
pip install SQL2Excel[mssql]
Snowflake
pip install SQL2Excel[snowflake]
Databricks
pip install SQL2Excel[databricks]
BigQuery
pip install SQL2Excel[bigquery]
Official Documentation
The official documentation is available here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sql2excel-0.1.0.tar.gz.
File metadata
- Download URL: sql2excel-0.1.0.tar.gz
- Upload date:
- Size: 48.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b13ab552fda3b4d2f50ec6fbabccb026333c7c98f0e68b41999c3f80daa08055
|
|
| MD5 |
29a6f6ade60e0f57b7c164d8563380dc
|
|
| BLAKE2b-256 |
44f0e8ebb7085ba8ae3a221a747582ac8a2d698da4d62f3cc6a80ddef8cc6af0
|
Provenance
The following attestation bundles were made for sql2excel-0.1.0.tar.gz:
Publisher:
publish-pypi.yml on Al-Madina/SQL2Excel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sql2excel-0.1.0.tar.gz -
Subject digest:
b13ab552fda3b4d2f50ec6fbabccb026333c7c98f0e68b41999c3f80daa08055 - Sigstore transparency entry: 2257719919
- Sigstore integration time:
-
Permalink:
Al-Madina/SQL2Excel@d6dd45f1401e43fe7ca2e37dbbab28b1a86ee580 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Al-Madina
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@d6dd45f1401e43fe7ca2e37dbbab28b1a86ee580 -
Trigger Event:
push
-
Statement type:
File details
Details for the file sql2excel-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sql2excel-0.1.0-py3-none-any.whl
- Upload date:
- Size: 38.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abd7fccef1ee88cdd3d38e60c974f8cb744a22aaa1488140eceb19800cbe1175
|
|
| MD5 |
6fd9331bc44548f7e3beebc37dd90a03
|
|
| BLAKE2b-256 |
a5a2841f84f4b99abcd1b201dc6dbddd897614bb24e4ed0a19e46799365be79e
|
Provenance
The following attestation bundles were made for sql2excel-0.1.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on Al-Madina/SQL2Excel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sql2excel-0.1.0-py3-none-any.whl -
Subject digest:
abd7fccef1ee88cdd3d38e60c974f8cb744a22aaa1488140eceb19800cbe1175 - Sigstore transparency entry: 2257720251
- Sigstore integration time:
-
Permalink:
Al-Madina/SQL2Excel@d6dd45f1401e43fe7ca2e37dbbab28b1a86ee580 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Al-Madina
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@d6dd45f1401e43fe7ca2e37dbbab28b1a86ee580 -
Trigger Event:
push
-
Statement type: