-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_to_html.py
67 lines (61 loc) · 1.64 KB
/
csv_to_html.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import csv
from jinja2 import Template
# Template HTML para a tabela
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSV to HTML</title>
<style>
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
{% for header in headers %}
<th>{{ header }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
"""
def csv_to_html(csv_file_path, html_file_path):
# Lê o arquivo CSV
with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
headers = next(reader) # Lê o cabeçalho
rows = list(reader) # Lê as linhas restantes
# Renderiza o template HTML com os dados do CSV
template = Template(html_template)
html_content = template.render(headers=headers, rows=rows)
# Salva o conteúdo HTML em um arquivo
with open(html_file_path, 'w', encoding='utf-8') as htmlfile:
htmlfile.write(html_content)
# Exemplo de uso
csv_to_html('vinho.csv', 'exemplo.html')