Files
parser_csv/csv_to_html.py
2022-10-04 11:46:01 +03:00

38 lines
2.1 KiB
Python

import os
import csv
start_dir = './Готовые отредактированные'
list_dir = os.listdir(start_dir)
for dir_name in list_dir:
if os.path.isfile(f'{start_dir}/{dir_name}') and dir_name[-3:] == 'csv':
html_doc_start = r'<!DOCTYPE html>' \
r'<html lang="ru">' \
r'<head>' \
r'<meta charset="UTF-8">' \
r'<title></title>' \
r'</head><body><table style="border: 1px solid black; border-collapse: collapse">'
html_doc_end = r'</table></body></html>'
html_body = ''
with open(f'{start_dir}/{dir_name}', encoding='utf-8') as f:
cnt_document = 0
parse_project = list(csv.reader(f, delimiter=';'))
for row in parse_project:
html_body += '<tr style="border: 1px solid black; border-collapse: collapse">'
for idx, item in enumerate(row):
if idx == 6:
html_body += f'<td style="border: 1px solid black; ' \
f'max-width: 800px; border-collapse: collapse; padding: 10px">{item}</td>'
elif idx == 8 and item != 'Прикрепленные файлы':
list_address = item.split('\n')
start_list_address = '<td style="border: 1px solid black; max-width: 800px; padding: 10px">'
stop_list_address = '</td>'
temp_list_address = ''
for link in list_address:
temp_list_address += f'<a href="{link}">{link}</a><br>'
html_body += start_list_address + temp_list_address + stop_list_address
else:
html_body += f'<td style="border: 1px solid black; padding: 10px">{item}</td>'
html_body += '</tr>'
with open(f'{start_dir}/{dir_name[:-4]}.html', 'w') as file_html:
file_html.write(html_doc_start + html_body + html_doc_end)