-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_builder.py
33 lines (26 loc) · 1 KB
/
html_builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pandas as pd
from bs4 import BeautifulSoup
def add_rows_to_html_table(df, html_file):
# Load the HTML file
with open(html_file, 'r') as file:
html_content = file.read()
# Parse the HTML content
soup = BeautifulSoup(html_content, 'html.parser')
# Find the table you want to append rows to (you might need to adjust this)
table = soup.find('table', {'id': 'sample_table'})
# Loop through the DataFrame and add rows to the table
for _, row in df.iterrows():
tr = soup.new_tag('tr')
for col in df.columns:
td = soup.new_tag('td')
td.string = str(row[col])
tr.append(td)
table.append(tr)
# Save the modified HTML content
with open('modified.html', 'w') as file:
file.write(str(soup))
# Example usage
data = {'Name': ['John', 'Jane', 'Jim'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
# Call the function with your DataFrame and HTML file
add_rows_to_html_table(df, 'path/to/default_html_file.html')