-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml2pdf.py
More file actions
34 lines (26 loc) · 1002 Bytes
/
html2pdf.py
File metadata and controls
34 lines (26 loc) · 1002 Bytes
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
# python code to convert html files in one directory into pdf files
# this was generated with the help of ChatGPT
import os
import pdfkit
# Specify the directory containing HTML files
html_dir = 'path/to/your/html/files/directory'
# Specify the output directory for PDF files
pdf_dir = 'path/to/output/pdf/files/directory'
# List all HTML files in the directory
html_files = [f for f in os.listdir(html_dir) if f.endswith('.html')]
# Make sure the output directory exists
if not os.path.exists(pdf_dir):
os.makedirs(pdf_dir)
# Initialize pdfkit options
options = {
'quiet': '',
}
for html_file in html_files:
# Build the input and output file paths
input_path = os.path.join(html_dir, html_file)
output_file = os.path.splitext(html_file)[0] + '.pdf'
output_path = os.path.join(pdf_dir, output_file)
# Convert HTML to PDF
pdfkit.from_file(input_path, output_path, options=options)
print(f'Converted {html_file} to {output_file}')
print("Conversion complete.")