You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.9 KiB
3.9 KiB
Markdown - HTML - print¶
In [ ]:
import pypandoc from weasyprint import HTML, CSS from weasyprint.fonts import FontConfiguration
Markdown → HTML¶
Pandoc: "If you need to convert files from one markup format into another, pandoc is your swiss-army knife."
The Python library for Pandoc:
Convert a Markdown file to HTML ...¶
In [ ]:
# ... directly from a file html = pypandoc.convert_file('language.md', 'html') print(html)
In [ ]:
# ... or from a pad from urllib.request import urlopen url = 'https://pad.xpub.nl/p/language/export/txt' response = urlopen(url) md = response.read().decode('UTF-8') with open('language.md', 'w') as f: f.write(md)
In [ ]:
html = pypandoc.convert_file('language.md', 'html') print(html)
HTML → PDF¶
for this we can use Weasyprint again
In [ ]:
html = HTML(string=html) print(html)
In [ ]:
css = CSS(string=''' @page{ size: A4; margin: 15mm; counter-increment: page; @top-left{ content: "hello?"; } @top-center{ content: counter(page); font-size: 7pt; font-family: monospace; color: blue; } @bottom-center{ content: "this is the bottom center!"; } } body{ color: magenta; } ''')
It's actually interesting and useful to have a close look at paged media properties in CSS:
https://developer.mozilla.org/en-US/docs/Web/CSS/%40page/size
In [ ]:
html.write_pdf('language.pdf', stylesheets=[css])
In [ ]: