DEV Community

Shivesh Tiwari
Shivesh Tiwari

Posted on

How to write a Leave Application, but using Python

This is a very beginner project, so don't worry if you have zero knowledge of programming but you want to create your own working program.

Ideation

So, as you all know that we have a specific format to write an application. In this post, I've chosen a leave application. Firstly, what we want is the sample of the application. Then, we are going to format the sample of the application with the important information. And at the end, we'll create a new file to output the text application and save it.

Prerequisites

A basic knowledge of file handling and string formatting.

The way I am going to make the program is using file handling in Python with string formatting. But you can also just make the use of string formatting to modify the program.

How ?

Firstly I saved a very raw and simple sample of application written in a file named, "letter.txt". I've used a simple format. Modify the file as per your requirements.

To
the Principal

{school_name}

Subject: Sick Leave Application

I am {name} of {class_name} of your institute. I am writing this
application to inform you that I am suffering from {reason} and
therefore, I want sick leave from school. I got this infection
last night and I will not be capable to come to the office for
from {start_date} for a several days. As advised by my doctor,
I should take rest and recover appropriately before continuing
work. The report from the doctor is also attached for your
reference.

Kindly grant me a leave for {no_days} days.

Yours Sincerely,

{name}
Enter fullscreen mode Exit fullscreen mode

Note: Just keep the data that is to be changed with the application inside "{ }" brackets. And use a single unique word to define the information used inside it. We are going to change this information using string formatting

Now create a Python script file, say "letter.py" and start to code.

Coding

Firstly, we are using the template saved in our file, "letter.txt". So, we'll have to use it, open and read the content inside it. We are going to use the concept of file handling here.

letter = open("letter.txt", "r")
text = letter.read()
Enter fullscreen mode Exit fullscreen mode

In these two lines, I've opened the file, in read mode (because we don't want to modify the file) and then I've read the content inside the file using read function and stored it in a variable, text.

Now, we have got the format in the text variable. Now, just replace those reference variables written inside the "{ }" brackets with the required data.

We're going to make the user input the required data. So, let's make some variables to let the user input some data.

name = str(input("Enter your name: "))
school_name = str(input("Enter your school's name: "))
class_name = str(input("Enter the class: "))
reason = str(input("Enter the reason for being absent: "))
start_date = str(input("Enter the date from which leave starts: "))
end_date = str(input("Enter the date from which leave ends: "))
no_days = str(input("Enter the no. of days: "))
Enter fullscreen mode Exit fullscreen mode

Note: I've used the same word references stored in the file inside "{ }" brackets. You can also do the same to remove any confusion.

Now, everything is well setup for us.

At last, We have to change the content in the text variable.

temp_str = f"{text}".format(name=name, school_name=school_name,
class_name=class_name, reason=reason, start_date=start_date,
end_date=end_date, no_days=no_days)

letter.close()
Enter fullscreen mode Exit fullscreen mode

I've created a variable and then changed its content with the text content of text variable, using string formatting. And after that in the same variable I've replaced the references with the variables.

This is a bit complicated to see as I've combined two steps in one. But, just look at it once and you will get it easily. If not, separate the two steps and print them one by one and see the difference.

At the end, close the opened file, "letter.txt".

Now, you have created your application using python. Print the "temp_str" variable and see the output.

Here, we are going to make a new output file to save the application.

So, we'll again use the concept of file handling.

output = open("output.txt", "w")

output.write(temp_str)

output.close()
Enter fullscreen mode Exit fullscreen mode

Congratulations! You have created a Python program to create your own application.

Now Run the script and see the magic...

Don't forget to like this post and Subscribe for more.
Till then, Bye Bye! See ya 😉

Top comments (0)