Invoice Automation System in Python - Full Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to build a complete invoice automation system with a graphical user interface and python so let us get right into it not [Music] AED so before we get into the coding I would like to give you a motivational preview up front of what we're going to end up with here this is our invoice automation application we have a couple of fields here that we can enter information into and then this is going to create a customized invoice and it's going to export it as a PDF so for example I can say uh the company I'm working with is the XYZ company then they live at Fantasy Street two3 uh the ZIP code is something like this the country USA let's say then the invoice number is something like this then the service description is teaching coding 2 hours and let's say 150 per hour and the payment method here is uh the main bank and then I just have to click on create invoice I can go to the desktop and then I can export this here as invoice. PDF then you can see invoice created and saved successfully and then I can go to the desktop and you can see the customized invoice so I have this template but I also have the information inside of it so XYZ company fantasy Street the ZIP code USA the invoice number also the the date then also the service description the amount the single price of $150 then also the full price so the calculation here two times $150 and also the payment method that I chose this is my main payment method if I choose a different one I'm going to have different bank details down there so this is what we're going to build in this video today all right so we're going to build an invoice automation system in Python today and the first step to do that is to create a template that contains the basic structure of our invoice alongside with some placeholders that we can spot and replace using our automation system to customize the invoice so in my case here I have this template. docx file I can open it in the files and then I can open it in only office here on Linux and you can see that this contains the basic structure of my invoice I have my logo in here I have some madeup company information that is always going to be the same this is about my company then I have here some placeholders like the partner so the company that I'm working with the partner Street the partner zip code and so on the date placeholder the amount placeholder the service description placeholder so everything that contains these uh square brackets is basically a placeholder and the rest is static it's always the same and you can see also we have some Bank data down here this is the docx file that I'm going to use as a template you can create whatever file you want just make sure that you have certain keywords that will be replaced so you want to have something like um amount in square brackets you can also use curly brackets it doesn't really matter you just need something that will be replaced by the automation system um yeah that that will be spotted in the template that's the basic idea and the rest will always stay the same so for this video today we're going to use doc X Files because we're going to use an external python package called docx which focuses on Doc x file so we're not going to use any other formats but this also works on Linux and we're going to convert them in the end to PDF so the output will be a PDF file but in between we're going to work with docx files for the customization and automation process the rest is going to be done in core python we're only going to use core python packages except for Doc X so what you need to do is you need to open up your command line pip or pip 3 install doc X and once you have that um we can or actually sorry I think this is not correct let me just double check I think I think docx is not the correct package I think it's called python docx yeah we need python docx so don't use docx pip 3 uninstall doc X use Python dox so pip 3 install python Das dox this is the package that we need in this video today all right so we're going to start by saying import OS we're going to also import subprocess we're going to import tkin as TK this is for the graphical user interface from TK inter we're going to import the file dialogue and from tkin we're also going going to import the message box and then also uh maybe let's do this up here import datetime sdt and then finally also import docx the external python package all right now our application will be a class so we're going to create a class invoice automation automation like this and in the init method we're basically going to construct the graphical user interface so we're going to say that there's a root window or a root application which is a TK instance and then to this root window we're going to add all the components so we're going to have a couple of labels we're going to have a couple of entries so text boxes we're going to have a drop-down menu and we're going to have a button to actually create the invoice so first of all we're going to say self root will have a title invoice Automation and it will have a geometry so a certain size and this size will be 500 600 all right so what I'm going to do now first is I'm going to add all the labels or I'm going to create all the labels I'm going to create all the um entries I'm going to create all the buttons and then I'm going to add them in the correct order so I'm going to say self. uh partner label and of course you need for each uh for each placeholder in your template you need to make sure that you have some value to put in there because of course you need to replace all of them so either you have um for each each placeholder a text box or for example in the case of a drop-down menu I will use a dictionary to map certain choices to a set of keywords I'm going to show you what I mean by that in a second here so the partner will be the first template so or the first placeholder in the template if I open this up again you will see we have the partner and that's the value of our first text box so I'm going to say partner label is going to be equal to a TK label which is going to have the text partner and that will be part of the root window so self root as a parent and then the text is going to be partner now we're going to do that for all the different placeholders that we want to input manually so for the partner label for the partner Street label for the partner uh ZIP City Country uh label so whatever you want to put in there then also for the invoice number label now this might be a little bit tedious because it's quite repetitive but for the service description label you can of course also do this yourself and just skip this part of the video for the service uh amount label and then also for the uh service single price label and of course for the payment method payment method label now the payment method method will be a uh drop-down choice so we're going to have our payment method but it will include the recipient the bank the uh bank number and the bank identification I'm I'm not sure actually what these acronyms stand for but this is uh basically your account number and this is your bank identifier or something uh but those four pieces of information will be combined in one selection in the drop- down menu uh so we're not going to input all of them uh separately or manually so here we're going to say street here we're going to say zip uh country or yeah zip country then um here we're going to say invoice number here we're going to say service description you were going to say service amount you were going to say service single price and here we're going to say payment Method All right so these are now just the labels they're just text showing what comes next and what comes next is going to be the entries now before we go to the entries into the drop down menu we're going to define the dictionary that contains our payment methods so we're going to say self. payment payment unor methods is going to be a dictionary that Maps certain choices certain uh strings to the information for uh this section in the template so so in my case for recipient bank and these two numbers um so what I'm going to do is I'm going to say I want to have a main bank and the main bank has the following values or the following mappings it's going to be the recipient uh is going to be uh neural 9 company now of course this should be if it's an actual invoice automation system the correct information I'm just going to make up something right now uh I'm going to have the bank being equal to hello world bank for example and I want to have the uh I'm not even sure what this is pronounced in English iban eban IB an whatever uh this value here is going to be uh XY 1 2 3 4 5 6 78 901 23 4 whatever and then the bank number or identifier will be ABCDE e f GH whatever so this is One bank account if I want to add another one I just copy paste this and change the values so I already have done this for three bank accounts so I'm going to copy paste this to not waste any more of your time in this video here so basically I just have main bank Second Bank Private Bank same idea here so I just changed the value slightly and then we have three different bank accounts that I can choose from the idea is instead of entering all of this information every time I can just choose which bank I want to be paid at uh so which bank the payment should go to and then I can just select it for the invoice all right so let's go to the entries we're going to say self. partner actually we can copy paste all of these here and we can change certain aspects so first of all the label should be changed to entry so I'm going to use a substitution here with Vim I'm going to say turn label to entry in this selection and also for the uppercase label I want to do the same thing so turn the uppercase label to uppercase entry and of course now we don't have any text anymore so I'm going to replace this as well here like that that's the basic idea and for most of the stuff this is actually what we need to do the only thing that we cannot leave like that is the payment method because that's not going to be an entry this is going to be a string variable and it's going to be linked to a drop- down menu to an option menu so we're going to say self. payment method is going to be equal to a TK string variable part of self root and we're going to say that the default value of the payment method is going to be set to main bank so to this one um but we will link this to a UI component the dropdown so self and we're going to say payment method dropdown is going to be equal to TK option menu which is part of self root which is connected to the pain payment method and we have the three options here I hope I'm not blocking this I'm not okay we have the three options here main bank Second Bank and private bank now I think we should actually also be able to do the same thing with the unpacking operators so I think we should be able to do something like um like payment methods or what was it self. payment methods do keys and then we can just unpack this however yeah I'm just going to do it like this [Music] um all right so we have that and then what we're going to do is we're going to also add the create button so self. create button is going to be a TK button part of self root it will have a text create invoice and it will be linked to a function the command is going to be equal to self. create invoice we don't have that yet so we're going to need to create it for now I'm just going to say create invoice will be a method and I will just put a pass so that we don't have to implement it yet um but this is our create button all right so we have all the UI elements we have labels we have entries we have a drop down we have a button all we need to do now is we need to add them all to the window so we need to say uh we want to have some padding options here so I don't have to repeat this every single line I'm going to say I always want to do fill X I always want to do expand being equal to true and I want to have a uh actually this needs to be a string sorry uh I always want to have a padding in the X direction or on x- axis of five and a padding the Y AIS of two that is now these are my options and all I want to do now is I want to say self do partner actually let me just copy all the UI elements so I don't have to write them out again manually so I have the labels and I also have the entries I'm going to put them down here what I want to do is I want to have a label and an entry I want to have uh where does this end this is here I want to have this and I want to put it after this and I want to have this and I want to put it after this I want to have this I want to put it after this this this after this this after this this after this then we have also our payment method dropdown and then we have our button now instead of creating these elements again after the equal sign I want to do a or actually before the equal sign I want to do a pack so I'm going to actually use a macro now to um to make this easier so I'm going to use a or actually let's not use a Vim Acro to not make it uh uninteresting for people that don't use Vim so I'm just going to go and say do pack and padding options and then I'm just going to use the repeat functionality that should be good enough um what's the issue here what do you mean on Partner entry should be a oh I think I deleted this this is not good sorry so this will take some time now again uh I think I didn't yeah I actually removed it from here I wanted to copy it okay so we need to do this again all right so let's do it quickly there you go there you go there you go there you go there you go there you go there you go and and also these two down here okay so again the only thing that we're going to do now is we're going to replace all of this stuff here by a pack with the padding options uh and I'm going to do that quickly by just using the dot functionality in Vim just repeat the last operation there you go I'm going to do this for all the UI elements and this should be it right okay so we added everything to our UI the only thing left to do is to say self. root. main Loop and then we can actually see if the UI works out so if the name is equal toore uncore maincore we're going to just create an instance of the invoice Automation and we should get this user interface it works it looks fine it doesn't have any functionality obviously but the UI is done so what we're going to do now is we're going to implement the process of actually um filling up the invoice with information so filling the template replacing the placeholders and then exporting all of this as a PDF file so the first thing we want to have is we want to have a static method um so I'm going to use a static method decorator here and this is going to be the replace text method and this method is just going to take a paragraph from Doc X later on and it's going to replace old text with new text that's the functionality here so we're going to say if old text in paragraph text because not always will you have this text um in every paragraph so only if the text is contained there what we want to do is we want to do paragraph text is equal to paragraph text. replace old text by new text nothing too fancy all right so this is a static method that we're going to use in our create invoice function we're not going to need any other functions everything else will be done in the create invoice function so the first thing we want to do here is we want to load the document using docx so we're going to say Doc x. document and we're going to load the template. docx file and then what we're going to do is we're going to get the selected uh payment method uh because next after this we're going to Define the whole replacement dictionary but to know how to replace the bank information we need to know which bank was selected so selected payment method is equal to self. payment methods and then self. payment method doget basic idea is we have this dictionary of payment methods and we want to get the one that our string variable up here where is it that this string variable has right now by being selected here in the dropdown that's the basic idea and what we do then is we try to define the following replacement mappings so we say Replacements is equal to a dictionary and this dictionary now will contain certain um certain Replacements first of all we want to replace the date and before we do that let me just add an accept block here uh we're going to Define this in a second but the first thing we want to do is we want to take the date and we want to replace it by the uh date of today in a certain format so we want to do DT date time today string format time and then we want to use the following format we want to use the year Dash the month Dash the day and of course you can change that to whatever format you like but I'm going to do it like this uh for the sake of this tutorial and for the most part the rest is quite simple all we have to do is we have to Define which one we're interested in partner for example and we have to replace it so remember we have all these keywords here partner partner Street partner Zip City Country invoice number service description all of them need to be replaced um and what we're going to do is we're going to say self. partner entry doget that's it and we're going to do that for all the different fields that we have we're going to do that for partner Street we're going to do that for partner Zip City Country you need to be accurate with the um with the placeholders here because otherwise they're not going to be replaced uh we need to do that with the invoice number we want to do that with the service description and the amount so service description amount and we want to do that with the single price we also want to do that with the full price which is not a part of the um of the entries here we need to do a calculation for this one so we're going to change that in a second and we want to do that for the bank name or actually it was just Bank we want to do the same for the uh actually first of all the recipient the bank then this account number and the bank identification and how we do that is we just replace this again by Street entry we replace this by partner Zip City Country entry replace this by invoice number entry we replace this by service description entry buy service amount entry now here um for the amount and for the single price entry what we want to do is we want to actually uh use something specific now for the amount we don't want to do actually anything but for the single price we want to use a formatted string where this is part of the formatted string but not all of it because we want to add a dollar sign in the beginning because I don't want to just have a number there I want to have a currency as well so and then I need to use um float of that value and I want to use this accurate up to two decimal places so0 2f so I'm taking the string I'm turning it into a float and then I'm showing it up to two decimal places with a dollar now for the full price what we want to do is we want to do a calculation so I want to do a formatted string I want to also use a dollar sign and then I want to use the following I want to say float of self. service amount entry doget um times float uh of self. service what was it Service uh single price entry. getet and the whole thing actually let me just think about this for a second yeah so we we do a get here as well we typ cast it we do the multiplication and in the end we do 02f as well so that's the calculation for the recipient we do selected payment method recipient actually let me just copy paste this it's easier and what's the problem here did I not close something yeah I didn't close something so I need to have a comma here in the end um I'm going to indent this and here we just going to replace again Bank this and this and then we're going to do the same thing here so Bank this and this there you go all right so these are now the mappings The Replacements everything you see on the left will be replaced by everything you see on the right so everything that is here should be covered by everything on the left and everything on the right should be provided by the application that's the basic idea now in the case that we have a value error because for some reason I cannot do the type casting here in this case I want to show a message box I want to show an error message box saying error invalid amount or price because the type casting didn't work so I get a value error and I want to return in this case so I don't want to finish um I I don't want to do anything else I just want to say it doesn't work stop and try again in the case everything worked what I want to do is I want to go through all the paragraphs so for paragraph in list doc paragraphs for all the paragraphs I want to replace the old text and by the new text so for old text and new text in our Replacements we're going to iterate over the replacement items here over these pairs old text new text and we're going to say uh self. replace text our static function and we want to replace in the paragraph that we currently have the old text with the new text now this is just a little bit more modula if you do it like this you can also do it directly uh down here but that's the basic idea and then we want to do the same thing for the tables because this here does not include the tables so what we want to do is we want to say for table in Doc tables for Row in table rows four cell in row cells so we go through all the cells and in the cells we might have paragraphs so four paragraph uh actually I think then we can just copy this here so we can say for paragraph in cell. paragraphs we do the same thing so that's the idea um so all The Replacements should be done in paragraphs and in the tables and then once all of this is replaced and done we say save path is equal to the user is allowed to choose a um destination so file dialogue. ask save as file name uh the default extension here is going to be PDF and the file types that are allowed here are going to be just one a tupal of PDF as a or let's call this PDF documents and this is going to allow uh everything. PDF that's the basic uh file type that we support here then we're going to save our doc file so we're going to save this at as filt do docx so this is going to be the template replaced all the values replaced filt and then we're going to save this as a doc x file and we're going to use Liber office to um convert this to a PDF file now on Windows you can also use a python package called doc X2 PDF you just have to install this using pip so pip install doc X2 PDF and then you can do from Doc X to PDF uh you can import convert then you can just call the convert function onto fil do docx and then you can pass the safe path this is how it works on Windows if you have word installed if you don't have word installed because you're on Linux you use Libra office so you open up your terminal you use your package manager to install Libra office so pseudo app install Libra office on Debian based distributions for example and then you can use it in the command line using subprocess so you can say sub process do run and you can say uh Libra office run this in headless mode so without a user interface then convert to PDF and then our document fil do docx then our out directory will be the current directory so just a DOT we going to say check check equals true and this is going to be the command to export this as a PDF and then we rename the PDF so we say OS rename the fil PDF which is going to be the default name we move that to the safe path rename is basically a move so we move the filed PDF file that is the result of Libra office converting the fil doc X to this PDF file we take this and we move this to the safe path that's basically it and if all of this works we say message box show info success invoice created and save successfully there you go and that is it this is our application let's see if it works we run this uh I can say now uh I don't know let's say we collaborate with uh I don't know pythonic company then the partner street is uh I don't know pythia Street one 2 three the ZIP code is something like this the country is uh python the invoice number is p something the service description is uh freelancing freelance development service amount is 5 hours of uh 200 per hour and I want to use my second Bank create the invoice I can choose a location I'm going to save it here in the current directory let's export this as invoice PDF save and I got a problem why is that uh must be an issue let me see if I mess up something with the command here Libra office headless convert oh actually this is convert dash2 not convertor 2 so I will have to re-enter all of this again let's go pythonic company pythia Street one two 3 two whatever some zip code with python as a country then invoice number is p something service description is freelance development 5 hours 200 and then Second Bank create invoice invoice PDF save and there you go invoice created and saved successfully if I try to do that with text here it says invalid amount or Price okay let's look at the invoice open in files and you will see we have a date we have pythonic company python street we have this we have the invoice number we have uh freelance development 5 * $200 $1,000 and we have our bank information here the Second Bank chosen this is how you build an invoice automation system in Python so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye
Info
Channel: NeuralNine
Views: 7,580
Rating: undefined out of 5
Keywords: python, invoice automation, automate invoice, python invoice automation, gui project, gui, graphical user interface, tkinter project, python gui project, python tkinter project
Id: JuBEC1RW8nA
Channel Id: undefined
Length: 31min 24sec (1884 seconds)
Published: Tue Jun 11 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.