Avatar of Dan Carp
Dan Carp
 asked on

Generate individual txt files from spreadsheet rows

I would like to generate individual HTML / TXT files with values from a spreadsheet / CSV to be used as email signature files.  We have a CSV consisting of:

NAME    | EXT | EMAIL
Person1 | 301 | person1@contoso.com
Person2 | 302 | person2@contoso.com
Person3 | 303 | person3@contoso.com

Open in new window

I would like to create a unique HTML file for each user / row, reading:

<html>
<head></head>
<body>   <div>     <p>     <strong>[NAME]</strong><br>     <a href="mailto:[EMAIL]">[EMAIL]</a><br>     Ext: [EXT]     </p>   </div> </body> </html>

Open in new window

I think this would be a fairly basic task, but am having trouble identifying the right approach (e.g. VB, Powershell, etc).  Could somebody help point me in the right direction?  Thank you very much in advance!

PowershellVB ScriptHTMLSpreadsheetsScripting Languages

Avatar of undefined
Last Comment
Norie

8/22/2022 - Mon
Andrei Fomitchev

Python:

fileIn = open('emailHTML.txt')
mailNo = 1
for row in fileIn:
    columns = row.split('|')
    for k in range(0,3):
        columns[k]=columns[k].replace('\n','').strip()
    if len(columns) == 3:
        name = 'eMail_'+str(mailNo)+'.html'
        fileOut = open(name,'w')
        fileOut.write(
"""<html>
<head></head>
<body>
  <div>
    <p>
    <strong>"""+columns[0]+"""</strong><br>
    <a href="mailto:"""+columns[2]+"""">"""+columns[2]+"""</a><br>
    Ext: """+columns[1]+"""
    </p>
  </div>
</body>
</html>
"""
        )
        fileOut.close()
        mailNo += 1
fileIn.close()
#############
Source file emailHTML.txt:

Person1 | 301 | person1@contoso.com
Person2 | 302 | person2@contoso.com
Person3 | 303 | person3@contoso.com

Result files: eMail_N.html:

<html>
<head></head>
<body>
  <div>
    <p>
    <strong>Person1</strong><br>
    <a href="mailto:person1@contoso.com">person1@contoso.com</a><br>
    Ext: 301
    </p>
  </div>
</body>
</html>

<html>
<head></head>
<body>
  <div>
    <p>
    <strong>Person2</strong><br>
    <a href="mailto:person2@contoso.com">person2@contoso.com</a><br>
    Ext: 302
    </p>
  </div>
</body>
</html>

<html>
<head></head>
<body>
  <div>
    <p>
    <strong>Person3</strong><br>
    <a href="mailto:person3@contoso.com">person3@contoso.com</a><br>
    Ext: 303
    </p>
  </div>
</body>
</html>



ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Norie

This Python script assumes the data is in a file named persons.csv and creates HTML files named person1.html, person2.html etc.
with open("persons.csv", "r") as in_file:
    data = in_file.readlines()

for person in data[1:]:
    name, ext, email = person.strip().split("|")
    with open(f"{name}.html", "w") as out_file:
        out_file.write(
            f"""<html>
<head></head> 
<body>
  <div>
    <p>
    <strong>{name}</strong><br>
    <a href="mailto:{email}">{email}</a><br>
    Ext: {ext}
    </p>
  </div>
</body>
</html>"""
        )

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes