Last active
January 29, 2025 20:16
-
-
Save fnx4/e2f28511817daeb8646e36addf64b656 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def create_html_file(input_dir, output_file): | |
with open(output_file, 'w', encoding='utf-8') as outfile: | |
outfile.write('''<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>HTML Book</title> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> | |
<style> | |
* { | |
font-family: sans-serif; | |
} | |
</style> | |
</head> | |
<body> | |
''') | |
for filename in os.listdir(input_dir): | |
if filename.endswith('.txt'): | |
with open(os.path.join(input_dir, filename), encoding='utf-8') as infile: | |
text = infile.read() | |
title = os.path.splitext(filename)[0] | |
outfile.write(f'<h1>{title}</h1>\n') | |
for line in text.splitlines(): | |
outfile.write(f'<p>{line}</p>\n') | |
outfile.write('</body></html>') | |
output_file = 'combined.htm' | |
create_html_file('.', output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment