Link to home
Start Free TrialLog in
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!

Avatar of Andrei Fomitchev
Andrei Fomitchev
Flag of United States of America image

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
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Norie
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