Sick Gaming
[Tut] How I Generate Invoices For My Clients Using Python - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] How I Generate Invoices For My Clients Using Python (/thread-100586.html)



[Tut] How I Generate Invoices For My Clients Using Python - xSicKxBot - 01-16-2023

How I Generate Invoices For My Clients Using Python

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1058500&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> <span class="kksr-muted">Rate this post</span> </div>
</p></div>
<figure class="wp-block-image size-full is-resized is-style-default"><img loading="lazy" decoding="async" src="https://blog.finxter.com/wp-content/uploads/2023/01/invoice.jpeg" alt="" class="wp-image-1058553" width="623" height="467" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/invoice.jpeg 1000w, https://blog.finxter.com/wp-content/uploads/2023/01/invoice-300x225.jpeg 300w, https://blog.finxter.com/wp-content/uploads/2023/01/invoice-768x576.jpeg 768w" sizes="(max-width: 623px) 100vw, 623px" /></figure>
<h2>Project Description</h2>
<p>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. </p>
<p>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 <strong>InvoiceGenerator API</strong>. </p>
<h2>Step 1: Installing and Importing the <a rel="noreferrer noopener" href="https://pypi.org/project/InvoiceGenerator/" target="_blank">InvoiceGenerator</a> Library</h2>
<p><code>InvoiceGenerator</code> 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. </p>
<p><strong><span style="text-decoration: underline">Installation</span></strong></p>
<p>To install the InvoiceGenerator library, open your terminal and run the following command – </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install InvoiceGenerator</pre>
<p>In case you want to upgrade to a new version, then use the <code>--upgrade</code> flag. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install InvoiceGenerator – upgrade</pre>
<p>Read more about this library and how to use the API <strong><a href="https://pypi.org/project/InvoiceGenerator/" target="_blank" rel="noreferrer noopener">here</a></strong>.</p>
<p>Once you have installed the library, go ahead and import it into your script. You also need to import the <code>os</code> module along with the necessary modules within the <code>InvoiceGenerator</code> library. Here’s a quick look at the necessary modules that we will need in our code – </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import os
from InvoiceGenerator.api import Invoice, Item, Client, Provider, Creator
from InvoiceGenerator.pdf import SimpleInvoice</pre>
<ul>
<li>Import <code>Invoice</code>, <code>Item</code>, <code>Client</code>, <code>Provider</code> and <code>Creator</code> from <code>InvoiceGenerator.API</code>.</li>
<li>Also, import <code>SimpleInvoice</code> from <code>InvoiceGenerator.PDF</code>.</li>
<li>Finally, import <code>os</code> for performing OS-related activities.</li>
</ul>
<h2>Step 2: Create the Automated PDF Invoice</h2>
<p>The next step is to create an automated PDF using the <code>InvoiceGenerator</code> API.</p>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)</pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>We will first set the document environment language. In my case, I have set it to “English”.</li>
<li>We then need to set the mandatory details like the client, provider and creator. The client details can be set using the <code>Client</code> object. Similarly, to set the provider, use the <code>Provider</code> object. I have set the provider name and the bank details within the Provider object. </li>
<li>Next, create the invoice object that will allow us to generate the numerous features within the bill. </li>
<li>I have then used an <a rel="noreferrer noopener" href="https://blog.finxter.com/python-input-function/" target="_blank">input()</a> function to allow the user to enter the total number of items he/she wants to include in the bill. </li>
<li>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.</li>
<li>All these details can then be implemented within the invoice PDF using the <code>item.add_item()</code> method. </li>
<li>You can further specify
<ul>
<li>The currency of transaction using <code>invoice.currency</code>, </li>
<li>The invoice number using <code>invoice.number</code></li>
</ul>
</li>
<li> Finally, generate the invoice PDF with the help of the <code>SimpleInvoice</code> objects <code>gen</code> method. To generate a QR code within the PDF use the <code>generate_qr_code</code> object.</li>
</ul>
<h2>Putting it All Together</h2>
<p>Let’s put it all together to visualize how the entire script works –</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)</pre>
<p>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.</p>
<h2>Conclusion</h2>
<p>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!? </p>
<p>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 <strong><a rel="noreferrer noopener" href="https://blog.finxter.com/subscribe/" target="_blank">subscribe</a></strong> for more interesting projects and tutorials.</p>
<p>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 –</p>
<ul>
<li><a href="https://blog.finxter.com/create-your-own-youtube-video-downloader/"><strong>Create Your Own YouTube Video Downloader</strong></a></li>
<li>&nbsp;<strong><a href="https://blog.finxter.com/the-power-of-automation-using-python-segregating-images-based-on-dimensions/" target="_blank" rel="noreferrer noopener">The Power of Automation Using Python – Segregating Images Based on Dimensions</a></strong></li>
<li><strong><a href="https://blog.finxter.com/automate-backup-to-google-drive-with-python/" target="_blank" rel="noreferrer noopener">Automate Backup to Google Drive with Python</a></strong></li>
<li><strong><a href="https://blog.finxter.com/receive-automated-email-notification-on-website-update/" target="_blank" rel="noreferrer noopener">Receive Automated Email Notification on Website Update</a></strong></li>
<li><strong><a href="https://blog.finxter.com/the-art-of-automating-whatsapp-messages-using-python/" target="_blank" rel="noreferrer noopener">The Art of Automating Whatsapp Messages Using Python</a></strong></li>
<li><strong><a rel="noreferrer noopener" href="https://blog.finxter.com/create-your-own-youtube-video-downloader/" target="_blank">Create Your Own YouTube Video Downloader</a></strong></li>
<li><strong><a rel="noreferrer noopener" href="https://blog.finxter.com/how-i-used-python-to-automate-my-daily-routine-with-desktop-notifications/" target="_blank">How I used Python to Automate my Daily Routine with Desktop Notifications</a></strong></li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://academy.finxter.com/university/article-summarization/" target="_blank" rel="noopener"><img decoding="async" loading="lazy" width="363" height="650" src="https://blog.finxter.com/wp-content/uploads/2022/05/image-309.png" alt="" class="wp-image-387314" srcset="https://blog.finxter.com/wp-content/uploads/2022/05/image-309.png 363w, https://blog.finxter.com/wp-content/uploads/2022/05/image-309-168x300.png 168w" sizes="(max-width: 363px) 100vw, 363px" /></a></figure>
</div>
</div>


https://www.sickgaming.net/blog/2023/01/15/how-i-generate-invoices-for-my-clients-using-python/