How I Generate Invoices For My Clients Using Python

Project Description

Being self-employed personnel means I regularly need to generate Invoices for my clients to receive the payments. Now, this is a manual task that I have to perform using excel sheets and then convert them into PDFs. That is why I came up with a script that would automate the entire task for me.

Thus, in this mini project, I will demonstrate how I generate PDF invoices that I send to my clients using a simple Python script that uses the InvoiceGenerator API.

Step 1: Installing and Importing the InvoiceGenerator Library

InvoiceGenerator is a Python library that is specifically designed to generate simple invoices. Presently, it supports invoice generation in PDF and XML formats. The PDF invoice generation is based on ReportLab.

Installation

To install the InvoiceGenerator library, open your terminal and run the following command –

pip install InvoiceGenerator

In case you want to upgrade to a new version, then use the --upgrade flag.

pip install InvoiceGenerator --upgrade

Read more about this library and how to use the API here.

Once you have installed the library, go ahead and import it into your script. You also need to import the os module along with the necessary modules within the InvoiceGenerator library. Here’s a quick look at the necessary modules that we will need in our code –

import os
from InvoiceGenerator.api import Invoice, Item, Client, Provider, Creator
from InvoiceGenerator.pdf import SimpleInvoice
  • Import Invoice, Item, Client, Provider and Creator from InvoiceGenerator.API.
  • Also, import SimpleInvoice from InvoiceGenerator.PDF.
  • Finally, import os for performing OS-related activities.

Step 2: Create the Automated PDF Invoice

The next step is to create an automated PDF using the InvoiceGenerator API.

Code:

os.environ["INVOICE_LANG"] = "en"
client = Client('Finxter')
provider = Provider('Shubham Sayon Consultancy Services', bank_account='XXX-XXXX-XXXXX', bank_code='2021')
creator = Creator('Shubham Sayon')
invoice = Invoice(client, provider, creator)
number_of_items = int(input("Enter the number of Items: "))
for i in range(number_of_items):
    units = int(input(f"Enter the number of units for item no.{i+1}: "))
    price_per_unit = int(input(f"Enter the price per unit of item no.{i+1}: "))
    description = input("Enter the name of item/product: ")
    invoice.add_item(Item(units, price_per_unit, description=description))

invoice.currency = "$"
invoice.number = "10393069"
document = SimpleInvoice(invoice)
document.gen("invoice.pdf", generate_qr_code=True)

Explanation:

  • We will first set the document environment language. In my case, I have set it to “English”.
  • We then need to set the mandatory details like the client, provider and creator. The client details can be set using the Client object. Similarly, to set the provider, use the Provider object. I have set the provider name and the bank details within the Provider object.
  • Next, create the invoice object that will allow us to generate the numerous features within the bill.
  • I have then used an input() function to allow the user to enter the total number of items he/she wants to include in the bill.
  • Once the user enters the number of items, this can be used a range of create a for loop within which we can ask the user to enter further details like the number of units for each item, the price per unit of each item and the description of each item.
  • All these details can then be implemented within the invoice PDF using the item.add_item() method.
  • You can further specify
    • The currency of transaction using invoice.currency,
    • The invoice number using invoice.number
  • Finally, generate the invoice PDF with the help of the SimpleInvoice objects gen method. To generate a QR code within the PDF use the generate_qr_code object.

Putting it All Together

Let’s put it all together to visualize how the entire script works –

import os
from InvoiceGenerator.api import Invoice, Item, Client, Provider, Creator
from InvoiceGenerator.pdf import SimpleInvoice

os.environ["INVOICE_LANG"] = "en"
client = Client('Finxter')
provider = Provider('Shubham Sayon Consultancy Services', bank_account='XXX-XXXX-XXXXX', bank_code='2021')
creator = Creator('Shubham Sayon')
invoice = Invoice(client, provider, creator)
number_of_items = int(input("Enter the number of Items: "))
for i in range(number_of_items):
    units = int(input(f"Enter the number of units for item no.{i+1}: "))
    price_per_unit = int(input(f"Enter the price per unit of item no.{i+1}: "))
    description = input("Enter the name of item/product: ")
    invoice.add_item(Item(units, price_per_unit, description=description))

invoice.currency = "$"
invoice.number = "10393069"
document = SimpleInvoice(invoice)
document.gen("invoice.pdf", generate_qr_code=True)

That’s it! As simple as that and when you execute the code it will generate the invoice PDF within your project folder conataining all the details mentioned by you.

Conclusion

Hurrah! We have successfully created an automation script that allows us to generate our customized PDF that also has a QR code embedded within it. Isn’t this extremely handy and useful!?

With that, we come to the end of this project.  I hope this project added some value and helped you in your coding quest. Stay tuned and subscribe for more interesting projects and tutorials.

Do you love automating tasks with Python? Well! I do. And if you are someone who likes automation, then here’s a list of a few mini projects that will get you going –