• Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    My Backup Setup for Linux PCs

    My Backup Setup for Linux PCs

    Detecting Hidden WordPress Malware Disguised as Images

    Detecting Hidden WordPress Malware Disguised as Images

    Server-Side Image Conversion with Apache

    Server-Side Image Conversion with Apache

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Monitor SSL Expiration with Python

    Monitor SSL Expiration with Python

    Building a Simple WordPress Post List Tool with PHP

    Building a Simple WordPress Post List Tool with PHP

    Monitoring Web Page Changes with Python

    Monitoring Web Page Changes with Python

    My SSH Setup: How I Manage Multiple Servers

    My SSH Setup: How I Manage Multiple Servers

    Building a Network Tracker Auditor for Privacy with Python

    Building a Network Tracker Auditor for Privacy with Python

  • Blog
    • All
    • Artificial Intelligence
    • Developer Life
    • Privacy
    • Reviews
    • Security
    • Tutorials
    Imposter Syndrome as a Self-Taught Developer

    Imposter Syndrome as a Self-Taught Developer

    Why Stable Websites Outperform Flashy Redesigns

    Why Stable Websites Outperform Flashy Redesigns

    AdGuard Ad Blocker Review

    AdGuard Ad Blocker Review

    Surfshark VPN Review

    Surfshark VPN Review

    Nmap Unleash the Power of Cybersecurity Scanning

    Nmap: Unleash the Power of Cybersecurity Scanning

    Floorp Browser Review

    Floorp Browser Review

    Understanding Man-in-the-Middle Attacks

    Understanding Man-in-the-Middle Attacks

    Privacy-Focused Analytics

    Privacy-Focused Analytics: Balancing Insights and Integrity

    Safeguarding Your Facebook Account

    Safeguarding Your Facebook Account: Understanding the Differences Between Hacking and Cloning

  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity
No Result
View All Result
  • Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    My Backup Setup for Linux PCs

    My Backup Setup for Linux PCs

    Detecting Hidden WordPress Malware Disguised as Images

    Detecting Hidden WordPress Malware Disguised as Images

    Server-Side Image Conversion with Apache

    Server-Side Image Conversion with Apache

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Fastest Way to Extract a Massive .tar.gz File on Linux

    Monitor SSL Expiration with Python

    Monitor SSL Expiration with Python

    Building a Simple WordPress Post List Tool with PHP

    Building a Simple WordPress Post List Tool with PHP

    Monitoring Web Page Changes with Python

    Monitoring Web Page Changes with Python

    My SSH Setup: How I Manage Multiple Servers

    My SSH Setup: How I Manage Multiple Servers

    Building a Network Tracker Auditor for Privacy with Python

    Building a Network Tracker Auditor for Privacy with Python

  • Blog
    • All
    • Artificial Intelligence
    • Developer Life
    • Privacy
    • Reviews
    • Security
    • Tutorials
    Imposter Syndrome as a Self-Taught Developer

    Imposter Syndrome as a Self-Taught Developer

    Why Stable Websites Outperform Flashy Redesigns

    Why Stable Websites Outperform Flashy Redesigns

    AdGuard Ad Blocker Review

    AdGuard Ad Blocker Review

    Surfshark VPN Review

    Surfshark VPN Review

    Nmap Unleash the Power of Cybersecurity Scanning

    Nmap: Unleash the Power of Cybersecurity Scanning

    Floorp Browser Review

    Floorp Browser Review

    Understanding Man-in-the-Middle Attacks

    Understanding Man-in-the-Middle Attacks

    Privacy-Focused Analytics

    Privacy-Focused Analytics: Balancing Insights and Integrity

    Safeguarding Your Facebook Account

    Safeguarding Your Facebook Account: Understanding the Differences Between Hacking and Cloning

  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity
No Result
View All Result
Home Guides Programming Python

Using XlsxWriter to Create an Excel Spreadsheet

Jonathan Moore by Jonathan Moore
5 years ago
Reading Time: 2 mins read
A A
Using XlsxWriter to Create an Excel Spreadsheet
FacebookTwitter

From time to time I find myself needing to use different tools to organize data. During the beginning of the COVID-19 pandemic, I needed to automate the creation of spreadsheets with data of the previous day’s COVID-19 cases. This led me to a Python module called XlsxWriter.

XlsxWriter is a Python module for writing files in the Excel 2007+ XLSX file format. It can be used to write text, numbers, and formulas to multiple worksheets and it supports features such as formatting, images, charts, page setup, auto filters, conditional formatting, and many others.

One thing to note, XlsxWriter cannot read or modify existing Excel XLSX files but supports more features than any of the alternative modules. In most cases, the files produced are 100% equivalent to files that were produced by Excel.

Extensive documentation is available for this module that covers everything from installation to working with VBA Macros and Pandas. There are also several examples sprinkled throughout the documentation to help you get started.

Learning new Python modules can be quite the task, so I am going to show you the simplest format that I have came up with to get you quickly started in creating a Workbook for Excel.

Before we can get started, you will need to install the XlsxWriter module:

pip install xlsxwriter

Now we are ready to move into the fun stuff, the code. Within the code, we will be creating a sample Workbook for Expenses that is complete with cell formatting and calculation.

#!/usr/bin/python3

import xlsxwriter
from datetime import datetime


def create_workbook(filename, expenses):
    # Create a Workbook with the Filename that was passed
    workbook = xlsxwriter.Workbook(filename + '.xlsx')

    # Add a Sheet to the Workbook
    worksheet = workbook.add_worksheet('Home Expenses')

    # Add Speadsheet Bold Format
    bold_text = workbook.add_format({'bold': True})

    # Add Speadsheet Money Format
    money_format = workbook.add_format({'num_format': '$#,##0'})

    # Add a Spreadsheet Date Format
    date_format = workbook.add_format({'num_format': 'MMMM DD, YYYY'})

    # Create bold Headers in Spreadsheet
    worksheet.write('A1', 'Item', bold_text)
    worksheet.write('B1', 'Date', bold_text)
    worksheet.write('C1', 'Cost', bold_text)

    # Adjust Item column width to fit the Item
    worksheet.set_column(0, 0, 20)

    # Adjust Date column width to fit Date
    worksheet.set_column(1, 1, 20)

    # Start with Row 1, Column 0
    row, col = 1, 0

    # Iterate through Nested Dictionary of expenses to get values
    for key, value in expenses.items():
        # Convert the Date string into a datetime object
        date = datetime.strptime(value['date'], "%Y-%m-%d")

        # Add the Item to the first Column
        worksheet.write(row, col, value['item'])

        # Add the Item Date to the second Column
        worksheet.write(row, col + 1, date, date_format)

        # Add the Item Cost to the third Column
        worksheet.write(row, col + 2, value['cost'], money_format)

        # Increment to the next Spreadsheet row
        row += 1

    # Add a row to calculate the Total
    worksheet.write(row + 1, 0, 'Total:', bold_text)

    # Use the SUM function to calculate the total cost
    worksheet.write_formula(row + 1, 2, "=SUM(C2:C8)", money_format)

    # Close and save the Workbook
    workbook.close()


if __name__ == "__main__":
    # A Nested Dictionary of Items to be added to the Spreadsheet
    expenses = {
        1: {"item": "Home Mortgage", "date": "2021-08-03", "cost": 350},
        2: {"item": "Cell Phone",    "date": "2021-08-12", "cost":  45},
        3: {"item": "Car Insurance", "date": "2021-08-15", "cost":  35},
        4: {"item": "Electricity",   "date": "2021-08-25", "cost": 400},
        5: {"item": "Cable TV/Net",  "date": "2021-08-30", "cost": 210},
        6: {"item": "Water PSD",     "date": "2021-08-30", "cost":  80},
        7: {"item": "Trash Pickup",  "date": "2021-08-30", "cost":  16}
    }

    # Create Workbook with 'MyExpenses' as filename and pass the Dictionary
    create_workbook("MyExpenses", expenses)
Tags: ProgrammingPython
ShareTweetSharePinShareShareScan
ADVERTISEMENT
Jonathan Moore

Jonathan Moore

I am a Software Architect and Senior Software Engineer with 30+ years of experience building applications for Linux and Windows systems. I focus on system architecture, custom web platforms, server infrastructure, and security-focused tools, with an emphasis on performance and reliability. Over the years, I have built everything from WordPress plugins and automation systems to full platforms, ad serving systems, monitoring tools, and API-driven applications. I prefer working close to the system, solving real problems, and building tools that are meant to be used.

Related Articles

Server-Side Image Conversion with Apache

Server-Side Image Conversion with Apache

I stopped relying on third party image services a while ago. They work, but they add cost, latency, and another...

Fastest Way to Extract a Massive .tar.gz File on Linux

Fastest Way to Extract a Massive .tar.gz File on Linux

When I am dealing with a 40GB or 50GB website backup, I do not just run tar -xzf file.tar.gz and...

Monitor SSL Expiration with Python

Monitor SSL Expiration with Python

SSL certificates are one of those systems that work quietly in the background until something goes wrong. When a certificate...

Next Post
Pi-hole

How to Block Ads Network-wide with Pi-hole

Recommended Services

Latest Articles

My Backup Setup for Linux PCs

My Backup Setup for Linux PCs

March 31st is World Backup Day. It is meant to be a reminder, but I do not rely on reminders...

Read moreDetails

Detecting Hidden WordPress Malware Disguised as Images

Detecting Hidden WordPress Malware Disguised as Images

With the recent discovery of malicious files like Stained_Heart_Red-600x500.png being found on servers across the web, it pushed me to...

Read moreDetails

Server-Side Image Conversion with Apache

Server-Side Image Conversion with Apache

I stopped relying on third party image services a while ago. They work, but they add cost, latency, and another...

Read moreDetails

Imposter Syndrome as a Self-Taught Developer

Imposter Syndrome as a Self-Taught Developer

I started writing code over 35 years ago. Everything I learned came from figuring things out on my own, long...

Read moreDetails
  • Privacy Policy
  • Terms of Service

© 2025 JMooreWV. All rights reserved.

No Result
View All Result
  • Home
  • Guides
    • Linux
    • Programming
      • JavaScript
      • PHP
      • Python
    • Tools
    • WordPress
  • Blog
    • Artificial Intelligence
    • Tutorials
    • Privacy
    • Security
  • Apps
    • Bible App
    • Bible Verse Screensaver
    • Blue AI Chatbot
    • Early Spring Predictor
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
    • Website Risk Level Tool
  • About
    • About JMooreWV
    • Live Cyber Attack Stats
  • Contact
    • General Contact
    • Website Administration & Cybersecurity