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.

7.7 KiB

Weasyprint

In [8]:
from weasyprint import HTML, CSS
from weasyprint.fonts import FontConfiguration
In [3]:
# If you use @font-face in your stylesheet, you would need Weasyprint's FontConfiguration()
font_config = FontConfiguration()

HTML

In [2]:
# small example HTML object
html = HTML(string='<h1>hello</h1>')

or in this case let's use python + nltk to make a custom HTML page with parts of speech used as CSS classes...

In [9]:
import nltk

with open('txt/LIQUID1.txt') as f:
    txt = f.read()
words = nltk.word_tokenize(txt)
tagged_words = nltk.pos_tag(words)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-9-52b984781443> in <module>
----> 1 import nltk
      2 
      3 with open('txt/LIQUID1.txt') as f:
      4     txt = f.read()
      5 words = nltk.word_tokenize(txt)

ModuleNotFoundError: No module named 'nltk'
In [ ]:
content = ''
content += '<h1>LIQUID | Rachel Armstrong</h1>'

for word, tag in tagged_words:
        content+= f'<span class="{tag}">{ word }</span> '
    
with open("txt/liquid.html", "w") as f:
    f.write(f"""<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="txt/liquid.css">
    

    
  
    <title>Liquid</title>
</head>
<body>
{content}
</body>
""")

html = HTML("txt/liquid.html")

Saved to 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.

Maybe useful evt. https://stackoverflow.com/questions/23358444/how-can-i-use-word-tokenize-in-nltk-and-keep-the-spaces

NB: The above HTML refers to the stylesheet language.css (notice that the path is relative to the HTML page, so no need to say txt in the link).

In [10]:
from IPython.display import IFrame
IFrame("txt/liquid.html", width=1024, height=600)
Out[10]:

Generating the PDF!

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).

In [44]:
## If we had not linked the CSS in the HTML, you could specify it in this way
# css = CSS("txt/language.css", font_config=font_config)
# html.write_pdf('txt/language.pdf', stylesheets=[css], font_config=font_config)
In [11]:
css = CSS("txt/liquid.css")
In [14]:
html.write_pdf('liquid1.pdf', stylesheets=[css], font_config=font_config)
In [15]:
from IPython.display import IFrame
IFrame("txt/liquid.pdf", width=1024, height=600)
Out[15]:
In [ ]:
 
In [ ]: