resources

How to Automatically Generate Invoices from a CSV File?

How to Automatically Generate Invoices from a CSV File

Introduction

We are very excited to cover yet another exciting automation project! More precisely, we will take on another example of how web automation can help a business.

In this blog post, we will provide you with a step-by-step tutorial on how to create a simple invoice generator using Python. We will shortly talk about what the software actually looks like, and further - how to code your very own invoice generator.

Note that while recording the following video our agency was still called "Self Made" :)

The Solution

Manual invoice generation and sending can be tedious processes. Fortunately, we can write a simple Python invoice generator script that automates them.

We ran across a job titled “Automated Invoice Generation Using API and Automated Emailing to Customers”. So, we decided to create a tutorial on how to do it. We also provided you with the needed documentation (GitHub repository).

This is what the client said about the job:

“I am trying to automate the Invoice generation process using. This website has an API. When I want to generate multiple invoices, I want this website to read all information for the fields from an Excel sheet, and accordingly generate multiple invoices at once. Please note the application must run on MAC OSX without the need of downloading IDEs.”‍

They have also provided us with a link to the website they want to use to automate the process. They have a CSV file with the invoicing information and they want to create PDF invoices from this CSV file.

This is the website whose API we want to use to automate the process. Here, our invoice generator will be inputting the information and the website will create the PDF invoices out of it.

At the bottom of the website, you can see an API button that will take us to the documentation of the website’s API. We will need it to automate invoice generation.

The link then proceeds with forwarding us to the GitHub page. On there we will find the relevant documentation. On this page, we will also see the endpoint - “https://invoice-generator.com”, and different examples - a curl input, a JSON input, as well as the VAT example.

However, we don’t need to focus too much here.

Preparation for writing the script

We first started by going into GitHub and creating a new repository.‍Then, we created and activated a virtual environment, as well as a simple Python file.

Last but not least, we created a directory for the invoices. This directory will hold the output of our CSV file. Let’s view it.

We have prepared a CSV file - you can access it on GitHub or create your own that matches your own needs.

In the CSV file, you can see the different categories:“from_who” - your company“to_who” - the client“logo” - the logo of your company“number” - the numeration of each invoice“date” - the date of each invoice“due date” - the due date of each invoice“items” - the services or products your company provides“notes” - some notes on the end

Coding the invoice generator

1. Import libraries

So, we will be importing requests - a data class. Whenever you want to represent something, you can use a data class for it.

In this tutorial, we will also be using types, as we will show you how to manage types in Python. Our data shows how we describe our object of invoice. These are the fields we will be taking from the CSV file and we will keep them here. So, we’ll have two classes - CSVParser and ApiConnector.

We will have the main logic here - underneath the two classes we just mentioned. The first class, CSVParser will be the one responsible for reading all the data from the CSV file and converting it to a list of invoice classes. Then, the APIConnector class will allow us to get the data class invoice and send it to the target API, get the data (the content of the PDF), and save it as a PDF file.

As you can see, if we type “python automate.py file.csv”, it will run with the “file.csv” already here. So, it is a super simple command-line interface.

2. Setting up API Connector

So, let’s start with the API Connector.

Reading and parsing the data is not less important at this point. Right now, we want to focus on “making the magic”. So, this is what creating types mean. Here, we can define what we accept as a return from this function and we want the init to return no values.

3. The two functions

Our invoice generator script will have two functions.

The first one will be a “bigger” one, and the second will be a more specific function that will use the first one. The first function is the one that connects and saves. The second one is for saving again.

As arguments, you can also see that we are passing the invoice, from which we expect these objects of the data class. Now, it’s very important to mention that we need to parse it. When we send the JSON file to the backend (the API), it accepts these fields here. Whenever we create a “from” field in Python, it’s already a used keyword so we have to go with “from_who”.

4. Testing the script with manually inputted information

We use prepared invoice information for testing the invoice generator. This is also the way we will be reading the invoice from the CSV file. Now, the second step would be to actually read this information from a CSV file, iterate over it and generate multiple invoices.

Okay, so this is how our function presents:‍We are opening the file - quite obvious.‍We are using the dictionary reader - a function for the CSV file.‍It takes the fields of the columns and creates the dictionary immediately after reading the file.

Let’s run it!

First, let’s focus on just reading the CSV file and getting the objects.

So, we do “python”, basically, if you do “automate.py” as an argument it will take “invoices.csv” but we can also type it here.

And, the script is done and running smoothly - generating multiple invoices, calculating “QTY” x “Amount” using the website’s API, and having no problems with multiple line items. We basically delivered a job post in UpWork and we really hope you find this article useful. As a blog post is hardly the best media for a coding tutorial, please make sure you watch the full video.

Summary

Now that you got to know the bone structure of an Invoice Generator you are able to enhance your business using web automation. You learned how to prepare for coding your very own script, as well as how to set up its functions and test it, as invoicing can be very tricky.‍

Need help?‍

If you need further assistance with automating a process or feel a personal consultation will make a difference please fill out the form.

Work with us
Let's build something amazing!
Fill out the contact form
Join the list of our satisfied clients. You’ll be in good company.