Coders Packet

Invitation Letter with different names using Python

By Sairam Patil

When you have to do tons of invitations and manually have to write names on each letter, it's hard work. Well not anymore, let Python do the work for you. You just write a list of names.

This project is done using Python Programming Language.
The major thing here is file handling using the open method.
with open('example.txt', mode='r') as file :
    file = file.read()
 

First, we open the file on which all the names are written, which we have to invite.

We've to open that file in reading mode and store its content in a variable called file.

This content includes everything including spaces, every letter, next line (\n). We've to convert it to a list of distinct names.
 
 
List of names in .txt file : 
List of names
 

So we use for loop to loop through file variable.

First above for loop we declare two variables to an empty list. They are name_collector and names.

So, while we loop through the file, we check if the upcoming  is  '\n'  or not if it's not then we append every element to names_collector,

and if it's equal to  '\n' then we to else statement and there we join every letter we've collected in name_collector and store it to a random variable a.

and append a to names. 

And like this, we'll get our Python list of names.
 
Code : 
Source code
 
 
As you can see the 'Ready to send' folder is empty.
 
Now again we use for loop, but this time we loop through names (list of names we collected).
we use n as an iterator.
Using f-string we store our actual letter in a variable called letter and the part where we have to write name, there we write {n}, so each time loop starts it uses a different name in place of {n}.
 
Now in the loop itself we again open the file in write mode, the specialty of write mode is that if the file doesn't exist in a given location it creates one and then write in it.
So, I've selected to store the letters in 'Ready to send' folder in working directory in ( .dox ) format.
 
After the execution of a program, there is a letter generated in ( .dox ) format in 'Ready to send' folder with each of the names on different letters.
 
Output : 
Output
 

Download Complete Code

Comments

No comments yet