"Weasyprint is a python library to layout HTML (and CSS) as print pages, saving to a PDF. In this way, it can be a part of a \"web to print\" workflow."
"The main class that weasyprint is HTML, it represents an HTML document, and provides functions to save as PDF (or PNG). When creating an HTML object you can specify the HTML either via HTML source as a string (via the *string* option), a file (via the *filename* option), or even an online page (via *url*)."
]
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"# small example HTML object\n",
"html = HTML(string='<h1>hello</h1>')"
"html = HTML(string='<h1>hello</h1>')"
]
]
},
},
@ -55,165 +63,173 @@
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
"source": [
"source": [
"or in this case let's use python + nltk to make a custom HTML page with parts of speech used as CSS classes..."
"or"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 42,
"execution_count": 4,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"import nltk\n",
"html = HTML(filename=\"path/to/some.html\")"
"\n",
"txt = open('txt/language.txt').read()\n",
"words = nltk.word_tokenize(txt)\n",
"tagged_words = nltk.pos_tag(words)"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "markdown",
"execution_count": 23,
"metadata": {},
"metadata": {},
"outputs": [],
"source": [
"source": [
"content = ''\n",
"or"
"content += '<h1>Language and Software Studies, by Florian Cramer</h1>'\n",
"\n",
"for word, tag in tagged_words:\n",
" content += f'<span class=\"{tag}\">{ word }</span> '\n",
"\n",
"with open(\"txt/language.html\", \"w\") as f:\n",
"Saved to [language.html](txt/language.html). Fun fact: jupyter filters HTML pages that are displayed in the notebook. To see the HTML unfiltered, use an iframe (as below), or right-click and select Open in New Tab in the file list.\n",
"NB: The above HTML refers to the stylesheet [language.css](txt/language.css) (notice that the path is relative to the HTML page, so no need to say txt in the link)."
"The CSS class lets you include an (additional) CSS file. Just as with the HTML class, you can give a string, filename, or URL. If the HTML already has stylesheets, they will be combined. (is this true?)"
"## Using NLTK to automatically markup a (plain) text with POS tags"
"\n",
"Now let's let weasyprint do it's stuff! Write_pdf actually calculates the layout, behaving like a web browser to render the HTML visibly and following the CSS guidelines for page media (notice the special rules in the CSS that weasy print recognizes and uses that the browser does not). Notice that the CSS file gets mentioned again explicitly (and here we need to refer to its path relative to this folder)."
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 39,
"execution_count": 9,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"## If we had not linked the CSS in the HTML, you could specify it in this way\n",