Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mail merge/ReadyToSend
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

30 changes: 22 additions & 8 deletions mail merge/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
#TODO: Create a letter using starting_letter.docx
#for each name in invited_names.txt
#Replace the [name] placeholder with the actual name.
#Save the letters in the folder "ReadyToSend".

#Hint1: This method will help you: https://www.w3schools.com/python/ref_file_readlines.asp
#Hint2: This method will also help you: https://www.w3schools.com/python/ref_string_replace.asp
#Hint3: THis method will help you: https://www.w3schools.com/python/ref_string_strip.asp
from docx import Document
import os


names_file_path = "Names/invited_names.txt"
template_letter_path = "letter/starting_letter.docx" # input letter template
output_directory = "output/ReadyToSend" # output folder


with open(names_file_path, "r") as file:
invited_names = file.readlines()
letter_template = Document(template_letter_path)


for invited_name in invited_names:
name = invited_name.strip()

personalized_letter = Document()
for paragraph in letter_template.paragraphs:
new_paragraph = personalized_letter.add_paragraph(paragraph.text.replace("[name]", name))

personalized_letter.save(f"{output_directory}/letter_for_{name}.docx")