├── .gitignore ├── README.md ├── Sales_Analysis.ipynb ├── create_database.py ├── output ├── customer_sales.png ├── monthly_sales.png ├── product_sales.png └── sales_report.pdf ├── pdf_output_demo.png ├── sales.db └── sales_db_schema.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.toptal.com/developers/gitignore/api/python 2 | # Edit at https://www.toptal.com/developers/gitignore?templates=python 3 | 4 | ### Python ### 5 | # Byte-compiled / optimized / DLL files 6 | __pycache__/ 7 | *.py[cod] 8 | *$py.class 9 | 10 | # C extensions 11 | *.so 12 | 13 | # Distribution / packaging 14 | .Python 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | downloads/ 19 | eggs/ 20 | .eggs/ 21 | lib/ 22 | lib64/ 23 | parts/ 24 | sdist/ 25 | var/ 26 | wheels/ 27 | share/python-wheels/ 28 | *.egg-info/ 29 | .installed.cfg 30 | *.egg 31 | MANIFEST 32 | 33 | # PyInstaller 34 | # Usually these files are written by a python script from a template 35 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 36 | *.manifest 37 | *.spec 38 | 39 | # Installer logs 40 | pip-log.txt 41 | pip-delete-this-directory.txt 42 | 43 | # Unit test / coverage reports 44 | htmlcov/ 45 | .tox/ 46 | .nox/ 47 | .coverage 48 | .coverage.* 49 | .cache 50 | nosetests.xml 51 | coverage.xml 52 | *.cover 53 | *.py,cover 54 | .hypothesis/ 55 | .pytest_cache/ 56 | cover/ 57 | 58 | # Translations 59 | *.mo 60 | *.pot 61 | 62 | # Django stuff: 63 | *.log 64 | local_settings.py 65 | db.sqlite3 66 | db.sqlite3-journal 67 | 68 | # Flask stuff: 69 | instance/ 70 | .webassets-cache 71 | 72 | # Scrapy stuff: 73 | .scrapy 74 | 75 | # Sphinx documentation 76 | docs/_build/ 77 | 78 | # PyBuilder 79 | .pybuilder/ 80 | target/ 81 | 82 | # Jupyter Notebook 83 | .ipynb_checkpoints 84 | 85 | # IPython 86 | profile_default/ 87 | ipython_config.py 88 | 89 | # pyenv 90 | # For a library or package, you might want to ignore these files since the code is 91 | # intended to run in multiple environments; otherwise, check them in: 92 | # .python-version 93 | 94 | # pipenv 95 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 96 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 97 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 98 | # install all needed dependencies. 99 | #Pipfile.lock 100 | 101 | # poetry 102 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 103 | # This is especially recommended for binary packages to ensure reproducibility, and is more 104 | # commonly ignored for libraries. 105 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 106 | #poetry.lock 107 | 108 | # pdm 109 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 110 | #pdm.lock 111 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 112 | # in version control. 113 | # https://pdm.fming.dev/#use-with-ide 114 | .pdm.toml 115 | 116 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 117 | __pypackages__/ 118 | 119 | # Celery stuff 120 | celerybeat-schedule 121 | celerybeat.pid 122 | 123 | # SageMath parsed files 124 | *.sage.py 125 | 126 | # Environments 127 | .env 128 | .venv 129 | env/ 130 | venv/ 131 | ENV/ 132 | env.bak/ 133 | venv.bak/ 134 | 135 | # Spyder project settings 136 | .spyderproject 137 | .spyproject 138 | 139 | # Rope project settings 140 | .ropeproject 141 | 142 | # mkdocs documentation 143 | /site 144 | 145 | # mypy 146 | .mypy_cache/ 147 | .dmypy.json 148 | dmypy.json 149 | 150 | # Pyre type checker 151 | .pyre/ 152 | 153 | # pytype static type analyzer 154 | .pytype/ 155 | 156 | # Cython debug symbols 157 | cython_debug/ 158 | 159 | # PyCharm 160 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 161 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 162 | # and can be added to the global gitignore or merged into this file. For a more nuclear 163 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 164 | #.idea/ 165 | 166 | ### Python Patch ### 167 | # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration 168 | poetry.toml 169 | 170 | # ruff 171 | .ruff_cache/ 172 | 173 | # End of https://www.toptal.com/developers/gitignore/api/python -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Transform Your Data into Stunning PDF Reports with Python and SQL 📊 2 | In this tutorial, I'll show you how to use Python and SQL to transform your data into stunning PDF reports. Whether you're an analyst, a business owner, or just looking to create professional-looking reports for your own use, this tutorial will guide you step-by-step through the process. 3 | 4 | First, I'll show you how to set up a SQLite database and load sample data into it using Python. Next, I'll demonstrate how to use SQL queries to extract the data you need from the database. Then, I'll use Pandas to manipulate and transform the data, and show you how to create interactive charts with Plotly Express. 5 | 6 | Once we have our data prepped and ready, I'll use the FPDF library to generate a beautiful PDF report. 7 | 8 | Whether you're a beginner or an experienced Python user, this tutorial will give you the skills you need to create stunning PDF reports with ease. Follow along, and transform your data into stunning reports that will impress your audience! 9 | 10 | ## Database Schema 11 | DB Schema 12 | 13 | ## PDF Output Example 14 | PDF Output Demo 15 | 16 | ## Video Tutorial 17 | [![YouTube Video](https://img.youtube.com/vi/LQywWpDoH9s/0.jpg)](https://youtu.be/LQywWpDoH9s) 18 | 19 | 20 | 21 | 22 | ## 🤓 Check Out My Excel Add-ins 23 | I've developed some handy Excel add-ins that you might find useful: 24 | 25 | - 📊 **[Dashboard Add-in](https://pythonandvba.com/grafly)**: Easily create interactive and visually appealing dashboards. 26 | - 🎨 **[Cartoon Charts Add-In](https://pythonandvba.com/cuteplots)**: Create engaging and fun cartoon-style charts. 27 | - 🤪 **[Emoji Add-in](https://pythonandvba.com/emojify)**: Add a touch of fun to your spreadsheets with emojis. 28 | - 🛠️ **[MyToolBelt Add-in](https://pythonandvba.com/mytoolbelt)**: A versatile toolbelt for Excel, featuring: 29 | - Creation of Pandas DataFrames and Jupyter Notebooks from Excel ranges 30 | - ChatGPT integration for advanced data analysis 31 | - And much more! 32 | 33 | 34 | 35 | ## 🤝 Connect with Me 36 | - 📺 **YouTube:** [CodingIsFun](https://youtube.com/c/CodingIsFun) 37 | - 🌐 **Website:** [PythonAndVBA](https://pythonandvba.com) 38 | - 💬 **Discord:** [Join our Community](https://pythonandvba.com/discord) 39 | - 💼 **LinkedIn:** [Connect with me](https://www.linkedin.com/in/sven-bosau/) 40 | - 📸 **Instagram:** [Follow me](https://www.instagram.com/codingisfun_official/) 41 | 42 | ## Support My Work 43 | Love my content and want to show appreciation? Why not [buy me a coffee](https://pythonandvba.com/coffee-donation) to fuel my creative engine? Your support means the world to me! 😊 44 | 45 | [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://pythonandvba.com/coffee-donation) 46 | 47 | ## Feedback 48 | Got some thoughts or suggestions? Don't hesitate to reach out to me at contact@pythonandvba.com. I'd love to hear from you! 💡 49 | ![Logo](https://www.pythonandvba.com/banner-img) 50 | -------------------------------------------------------------------------------- /create_database.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | import random 3 | import datetime 4 | 5 | # Define SQL commands for creating the tables 6 | create_sales_table = '''CREATE TABLE sales ( 7 | sale_id INTEGER PRIMARY KEY, 8 | sale_date DATE, 9 | customer_id INTEGER, 10 | product_id INTEGER, 11 | quantity INTEGER, 12 | unit_price DECIMAL(10, 2), 13 | total_price DECIMAL(10, 2), 14 | FOREIGN KEY (customer_id) REFERENCES customers(customer_id), 15 | FOREIGN KEY (product_id) REFERENCES products(product_id) 16 | )''' 17 | 18 | create_products_table = '''CREATE TABLE products ( 19 | product_id INTEGER PRIMARY KEY, 20 | product_name TEXT, 21 | unit_cost DECIMAL(10, 2) 22 | )''' 23 | 24 | create_customers_table = '''CREATE TABLE customers ( 25 | customer_id INTEGER PRIMARY KEY, 26 | first_name TEXT, 27 | last_name TEXT, 28 | email TEXT, 29 | phone TEXT 30 | )''' 31 | 32 | # Define SQL commands for inserting sample data into the tables 33 | insert_products_data = '''INSERT INTO products (product_name, unit_cost) VALUES (?, ?)''' 34 | 35 | insert_sales_data = '''INSERT INTO sales (sale_date, customer_id, product_id, quantity, unit_price, total_price) VALUES (?, ?, ?, ?, ?, ?)''' 36 | 37 | insert_customers_data = '''INSERT INTO customers (first_name, last_name, email, phone) VALUES (?, ?, ?, ?)''' 38 | 39 | # Define sample data for the products and customers tables 40 | products = [('Product A', 50.00), ('Product B', 25.00), ('Product C', 75.00), ('Product D', 40.00), ('Product E', 60.00)] 41 | 42 | customers = [ 43 | ('John', 'Doe', 'johndoe@example.com', '555-1234'), 44 | ('Jane', 'Doe', 'janedoe@example.com', '555-5678'), 45 | ('Bob', 'Smith', 'bobsmith@example.com', '555-9012'), 46 | ('Alice', 'Jones', 'alicejones@example.com', '555-3456'), 47 | ('David', 'Brown', 'davidbrown@example.com', '555-7890'), 48 | ('Emily', 'Davis', 'emilydavis@example.com', '555-2345'), 49 | ('Frank', 'Wilson', 'frankwilson@example.com', '555-6789'), 50 | ('Grace', 'Lee', 'gracelee@example.com', '555-1234'), 51 | ('Henry', 'Chen', 'henrychen@example.com', '555-5678'), 52 | ('Isabel', 'Garcia', 'isabelgarcia@example.com', '555-9012') 53 | ] 54 | 55 | # Define the start and end dates for generating sales data 56 | start_date = datetime.date(2022, 1, 1) 57 | end_date = datetime.date(2022, 12, 31) 58 | 59 | # Connect to the database and create the tables 60 | with sqlite3.connect('sales.db') as conn: 61 | # Create the sales table 62 | conn.execute(create_sales_table) 63 | 64 | # Create the products table 65 | conn.execute(create_products_table) 66 | 67 | # Create the customers table 68 | conn.execute(create_customers_table) 69 | 70 | # Insert sample data into the products table 71 | for product in products: 72 | conn.execute(insert_products_data, product) 73 | 74 | # Insert sample data into the customers table 75 | for customer in customers: 76 | conn.execute(insert_customers_data, customer) 77 | 78 | # Insert sample data into the sales table 79 | for i in range(1000): 80 | sale_date = start_date + datetime.timedelta(days=random.randint(0, 364)) 81 | customer_id = random.randint(1, len(customers)) 82 | product_id = random.randint(1, len(products)) 83 | quantity = random.randint(1, 10) 84 | unit_price = products[product_id-1][1] 85 | total_price = quantity * unit_price 86 | conn.execute(insert_sales_data, (sale_date, customer_id, product_id, quantity, unit_price, total_price)) 87 | 88 | # Commit the changes 89 | conn.commit() 90 | 91 | print("Database successfully created!") 92 | 93 | -------------------------------------------------------------------------------- /output/customer_sales.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/PDF-Report-Generator-Using-Python-and-SQL/488c3ef8790926c700d668adb3e2eb461c1d365e/output/customer_sales.png -------------------------------------------------------------------------------- /output/monthly_sales.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/PDF-Report-Generator-Using-Python-and-SQL/488c3ef8790926c700d668adb3e2eb461c1d365e/output/monthly_sales.png -------------------------------------------------------------------------------- /output/product_sales.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/PDF-Report-Generator-Using-Python-and-SQL/488c3ef8790926c700d668adb3e2eb461c1d365e/output/product_sales.png -------------------------------------------------------------------------------- /output/sales_report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/PDF-Report-Generator-Using-Python-and-SQL/488c3ef8790926c700d668adb3e2eb461c1d365e/output/sales_report.pdf -------------------------------------------------------------------------------- /pdf_output_demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/PDF-Report-Generator-Using-Python-and-SQL/488c3ef8790926c700d668adb3e2eb461c1d365e/pdf_output_demo.png -------------------------------------------------------------------------------- /sales.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/PDF-Report-Generator-Using-Python-and-SQL/488c3ef8790926c700d668adb3e2eb461c1d365e/sales.db -------------------------------------------------------------------------------- /sales_db_schema.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sven-Bo/PDF-Report-Generator-Using-Python-and-SQL/488c3ef8790926c700d668adb3e2eb461c1d365e/sales_db_schema.png --------------------------------------------------------------------------------