From ea7c743c3252d9f5ab6179c0b77fbe1d50a4ae8a Mon Sep 17 00:00:00 2001 From: Dready1 <149037835+Dready1@users.noreply.github.com> Date: Thu, 7 Nov 2024 10:14:34 +0200 Subject: [PATCH 1/2] Create ReadyToSend --- mail merge/ReadyToSend | 1 + 1 file changed, 1 insertion(+) create mode 100644 mail merge/ReadyToSend diff --git a/mail merge/ReadyToSend b/mail merge/ReadyToSend new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/mail merge/ReadyToSend @@ -0,0 +1 @@ + From f26474235251a2073532bec07fe4091253466bca Mon Sep 17 00:00:00 2001 From: Dready1 <149037835+Dready1@users.noreply.github.com> Date: Thu, 7 Nov 2024 10:16:00 +0200 Subject: [PATCH 2/2] Update main.py --- mail merge/main.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/mail merge/main.py b/mail merge/main.py index adb1ed1..f3186e1 100644 --- a/mail merge/main.py +++ b/mail merge/main.py @@ -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 \ No newline at end of file +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")