Skip to content

Instantly share code, notes, and snippets.

@pleabargain
Last active September 5, 2024 08:25
Show Gist options
  • Save pleabargain/85953d4e332a35f6188477fd01399ebe to your computer and use it in GitHub Desktop.
Save pleabargain/85953d4e332a35f6188477fd01399ebe to your computer and use it in GitHub Desktop.
simple python form filler. supply a form with text in [brackets] create a csv with headers with matchin [brackets] and run the code.
import csv
import os
def read_csv(filename):
data = []
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if any(row.values()): # Check if the row is not empty
data.append(row)
return data
def read_template(filename):
with open(filename, 'r') as file:
return file.read()
def generate_email(template, data):
return template.replace('[Your Name]', data['Your Name']) \
.replace('[Your Title]', data['Your Title']) \
.replace('[Company Name]', data['Company Name']) \
.replace('[Client Name]', data['Client Name'])
def save_email(content, client_name, script_dir):
filename = f"{client_name}.txt"
filepath = os.path.join(script_dir, filename)
with open(filepath, 'w') as file:
file.write(content)
return filepath
def main():
script_dir = os.path.dirname(os.path.abspath(__file__))
csv_file = os.path.join(script_dir, 'companyname.csv')
template_file = os.path.join(script_dir, 'formletter.txt')
data_list = read_csv(csv_file)
template = read_template(template_file)
generated_files = []
for data in data_list:
email_content = generate_email(template, data)
filepath = save_email(email_content, data['Client Name'], script_dir)
generated_files.append((data['Client Name'], filepath))
print(f"Number of documents produced: {len(generated_files)}")
for client_name, filepath in generated_files:
print(f"Document name: {client_name}.txt")
print(f"Document location: {filepath}")
print()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment