Put every file into a pdf

At the moment it takes all the text files and png files and makes an html and a pdf out of them. Good to make fast packages of lots of related files into a folder. In a future this could be an ebook maker.

import pdfkit
from glob import glob
import os


html = """
<style>

body {font-size: 2em;}
</style>

"""
for file in glob("*.*"):
    if file.endswith(".txt"):
        print(file)
        with open(file) as temp:
            lines = temp.readlines()
            for n, l in enumerate(lines):
                lines[n] = "<p>" + l + "</p>"
        html += "".join(lines)
    elif file.endswith(".PNG") or file.endswith(".png"):
        html += "<img src='" + os.getcwd() + "\\" + file + "' />"

print(html)
with open("output.html", "w", encoding="utf-8") as file:
    file.write(html)
pdfkit.from_string(html, "output.pdf")
os.startfile("output.pdf")

 

Live coding video

 

 


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.