• Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    Creating A Command-Line Python Radio Player

    Creating a Command-Line Python Radio Player

    Malware Detection With Maldet

    Malware Detection with Maldet

    Understanding And Implementing Clamav On Linux

    Understanding and Implementing ClamAV on Linux

    Simplifying Sms Alerts With Python And Smtp

    Simplifying SMS Alerts with Python and SMTP

    Automating Javascript Minification With Php

    Automating JavaScript Minification with PHP

    Secure Password Hashing

    Secure Password Hashing with PHP

    Synonym Word Replacer

    Create a Synonym Word Replacer with JavaScript

    Converting Images To Webp With Php

    Converting Images to WebP with PHP

    Verifying Artist Names In Mp3 Files Using The Last.fm Api With Python

    Verifying Artist Names in MP3 Files Using the Last.fm API with Python

  • Blog
    • All
    • Artificial Intelligence
    • Privacy
    • Python
    • Security
    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

    38 Essential Points To Harden Wordpress

    38 Essential Points to Harden WordPress

    10 Tips And Tricks To Secure Your Wordpress Website

    10 Tips and Tricks to Securing Your WordPress Website

    Ai And Coding: Will Ai Replace Coders?

    AI and Coding: Will Artificial Intelligence Replace Coders?

    Creating Strong Passwords

    Creating Strong Passwords: Factors, Best Practices, and Mistakes to Avoid

    Secure Your Online Life With Bitwarden

    Secure Your Online Life with Bitwarden

    Install Python 3 On Windows 10

    How to Install Python 3 on Windows 10

  • Services
    • WordPress Care Plans
    • WordPress Technical Support
  • Apps
    • Bible App
    • Blue AI Chatbot
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact
No Result
View All Result
  • Home
  • Guides
    • All
    • Linux
    • Programming
    • Tools
    • WordPress
    Creating A Command-Line Python Radio Player

    Creating a Command-Line Python Radio Player

    Malware Detection With Maldet

    Malware Detection with Maldet

    Understanding And Implementing Clamav On Linux

    Understanding and Implementing ClamAV on Linux

    Simplifying Sms Alerts With Python And Smtp

    Simplifying SMS Alerts with Python and SMTP

    Automating Javascript Minification With Php

    Automating JavaScript Minification with PHP

    Secure Password Hashing

    Secure Password Hashing with PHP

    Synonym Word Replacer

    Create a Synonym Word Replacer with JavaScript

    Converting Images To Webp With Php

    Converting Images to WebP with PHP

    Verifying Artist Names In Mp3 Files Using The Last.fm Api With Python

    Verifying Artist Names in MP3 Files Using the Last.fm API with Python

  • Blog
    • All
    • Artificial Intelligence
    • Privacy
    • Python
    • Security
    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

    38 Essential Points To Harden Wordpress

    38 Essential Points to Harden WordPress

    10 Tips And Tricks To Secure Your Wordpress Website

    10 Tips and Tricks to Securing Your WordPress Website

    Ai And Coding: Will Ai Replace Coders?

    AI and Coding: Will Artificial Intelligence Replace Coders?

    Creating Strong Passwords

    Creating Strong Passwords: Factors, Best Practices, and Mistakes to Avoid

    Secure Your Online Life With Bitwarden

    Secure Your Online Life with Bitwarden

    Install Python 3 On Windows 10

    How to Install Python 3 on Windows 10

  • Services
    • WordPress Care Plans
    • WordPress Technical Support
  • Apps
    • Bible App
    • Blue AI Chatbot
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact
No Result
View All Result
Home Guides Programming Python

Using XlsxWriter to Create an Excel Spreadsheet

Jonathan Moore by Jonathan Moore
2 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

Senior Software Engineer and Cybersecurity Specialist with over 3 decades of experience in developing web, desktop, and server applications for Linux and Windows-based operating systems. Worked on numerous projects, including automation, artificial intelligence, data analysis, application programming interfaces, intrusion detection systems, streaming audio servers, WordPress plugins, and much more.

Related Articles

Creating A Command-Line Python Radio Player

Creating a Command-Line Python Radio Player

In this tutorial, I'll walk you through the process of creating a command-line radio player using Python and ffplay, a...

Simplifying Sms Alerts With Python And Smtp

Simplifying SMS Alerts with Python and SMTP

Whether it's for emergency notifications or simple reminders, sending SMS alerts is a powerful way to get your message across....

Synonym Word Replacer

Create a Synonym Word Replacer with JavaScript

Writing compelling and engaging content often requires finding the perfect words to convey our thoughts. However, occasionally we find ourselves...

Next Post
Pi-Hole

How to Block Ads Network-wide with Pi-hole

Recommended Services

Subscribe for Updates

Would you like to be notified when a new article is published? You can unsubscribe at any time.

Subscribe

Latest Articles

Understanding Man-in-the-Middle Attacks

Understanding Man-In-The-Middle Attacks

Man-in-the-middle (MitM) attacks have earned a notorious reputation as one of the most insidious and potentially devastating cybersecurity threats today....

Read more

Creating a Command-Line Python Radio Player

Creating A Command-Line Python Radio Player

In this tutorial, I'll walk you through the process of creating a command-line radio player using Python and ffplay, a...

Read more

Malware Detection with Maldet

Malware Detection With Maldet

Malware, short for malicious software, is a ubiquitous threat to all computer systems. It's the covert intruder that sneaks into...

Read more

Understanding and Implementing ClamAV on Linux

Understanding And Implementing Clamav On Linux

The perpetual arms race between threat actors and defenders often hinges on the tools each employs. For Linux users, one...

Read more
473 Spam Attacks blocked with CleanTalk
  • Privacy Policy
  • Terms of Service

© 2023 JMooreWV. All rights reserved.

No Result
View All Result
  • Home
  • Guides
    • Linux
    • Programming
      • JavaScript
      • PHP
      • Python
    • Tools
    • WordPress
  • Blog
    • Artificial Intelligence
    • Python
    • Privacy
    • Security
  • Services
    • Privacy Focused Web Analytics
    • WordPress Care Plans
    • WordPress Technical Support
  • Apps
    • Bible App
    • Blue AI Chatbot
    • FIGlet Generator
    • Password Generator
    • StegX
    • The Matrix
    • WeatherX
  • About
    • About JMooreWV
    • Live Cyber Attacks
  • Contact