Python Forum
Populating list items to html code and create individualized html code files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Populating list items to html code and create individualized html code files
#1
I need to create a bunch of individualized e-mail signatures in html from data collected in an Excel file. I need a separate html/txt file as output for each signature to which the data from the excel file is populated to. I think this should be achievable with Python (flask and jinja2) and a html template. However, my knowledge of Python is unfortunately still very basic. Therefore, I would be very interested and grateful for some tipps and help in how such task could be achieved.

My project structure at the moment is as foillows:

/data.xlsx
/signature.py
  /templates/sig.html
  /output/
The simplified content of my excel file named data.xlsx looks like this within the columns A:C and rows 1:3

name_given  |  name  |  mail
==========================================
John        |  Doe   | [email protected]
Steve       |  Jobs  | [email protected]
My simplified html template is filed under sig.html and looks like this:

<!DOCTYPE html>
<html lang="de" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<HEAD>
    <head>
        <some css code>
    </head>
</HEAD>
<body width="100%" style="margin: 0; padding: 0 !important; mso-line-height-rule: exactly;">
    <left role="article" aria-roledescription="email" lang="de" style="width: 100%; ">
    <!--[if mso | IE]>
    <table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%" >
      <tr>
        <td>
            {{ name_given }} {{ name }}
        </td>
      </tr>
      <tr>
        <td>
          {{ mail }}
        </td>
      </tr>
    </table>
    </left>
</BODY>
</html>
I would like the following to happen:
  1. Python should read the data from data.xlsx with pandas to a dataframe.
  2. Every attribute within the dataframe is converted to separate list which is assigned to a variable.
  3. The variables populated by the step before are parsed to the html template (sig.html) or maybe better a virtual copy of the html template file. The variables are already found in the html code and are enclosed in two pair of curly brackets, eg. {{ foo }}.
  4. After the first item in each list from step 2 has been populated to the (virtual copy of) the html template its content should be saved in a new html or txt file which should be placed into the output folder. This seems to be possible by the implementation of a for-loop which could also be used to name the resulting html/txt files consecutively.

My very basic start of this looks like this:

from flask import Flask, render_template
import pandas as pd

workbook = pd.read_excel('data.xlsx', usecols='A:C')
workbook.head()

# print(workbook)
df = pd.DataFrame(workbook)

name_given = df['name_given'].values.tolist()
name = df['name'].values.tolist()
mail = df['mail'].values.tolist()

app = Flask(__name__)
@app.route('/', methods=['GET'])

def sig(name=None):
        return render_template('sig.html', name_given=name_given, name=name, mail=mail)
Any input is highly appreciated.

If you would like me to attache sample files as attachments please let me know.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Div Class HTML selector in Python Artur 1 622 Mar-28-2024, 09:46 AM
Last Post: StevenSnyder
  Trying to scrape data from HTML with no identifiers pythonpaul32 2 871 Dec-02-2023, 03:42 AM
Last Post: pythonpaul32
  How to find element in a deeply nested html structure Orientation_group 5 1,318 Oct-09-2023, 10:13 AM
Last Post: Larz60+
  Flask - use bootstrap styles in HTML Krayna 1 1,068 Aug-29-2023, 02:33 PM
Last Post: menator01
  Web scraper not populating .txt with scraped data BlackHeart 5 1,522 Apr-03-2023, 05:12 PM
Last Post: snippsat
  Getting a URL from Amazon using requests-html, or beautifulsoup aaander 1 1,678 Nov-06-2022, 10:59 PM
Last Post: snippsat
  selenium returns junk instead of html klaarnou 5 2,258 Mar-27-2022, 07:20 AM
Last Post: klaarnou
  requests-html + Beautifulsoup klaarnou 0 2,449 Mar-21-2022, 05:31 PM
Last Post: klaarnou
  Post Python to HTML/CSS Extra 3 1,909 Feb-17-2022, 06:07 PM
Last Post: snippsat
Question Python Obstacles | Jeet-Kune-Do | BS4 (Tags > MariaDB) [URL/Local HTML] BrandonKastning 0 1,428 Feb-08-2022, 08:55 PM
Last Post: BrandonKastning

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020