Clone
2
Home
Jens Tirsvad Nielsen edited this page 2026-10-13 14:22:57 +08:00

Mail Merge

🌐 English · Dansk · Bahasa Melayu

A small Python script from the Udemy 100 Days of Code course (day 24). It turns one letter template and a list of names into one personalised letter per name.

What it does

  1. Reads the guest names from Input/Names/invited_names.txt with readlines().
  2. Reads the letter template from Input/Letters/starting_letter.txt.
  3. For each name, removes the trailing newline with strip() and swaps the [name] placeholder with replace().
  4. Writes the result to Output/ReadyToSend/letter_for_<name>.txt.

Folder layout

main.py
Input/
  Names/invited_names.txt        one name per line
  Letters/starting_letter.txt    template containing [name]
Output/
  ReadyToSend/                   generated letters

How to run

From the project root:

python main.py

The Output/ReadyToSend/ folder must exist before you run it. It is created in the repository and contains the generated letters.

Adapting it

  • Different guests: edit Input/Names/invited_names.txt, one name per line.
  • Different letter: edit Input/Letters/starting_letter.txt and keep the [name] placeholder.
  • Different placeholder: change PLACEHOLDER at the top of main.py.

Python methods used

Method Purpose
readlines() Read all names from a file into a list
strip() Remove the newline and surrounding whitespace from each name
replace() Substitute the placeholder with the name