From ebac31033eebfcfc19090098b7c6626e301f92ca Mon Sep 17 00:00:00 2001 From: manetta Date: Mon, 26 Oct 2020 17:03:13 +0100 Subject: [PATCH] adding a weasyprint notebook --- weasyprint.ipynb | 283 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 weasyprint.ipynb diff --git a/weasyprint.ipynb b/weasyprint.ipynb new file mode 100644 index 0000000..cc4b028 --- /dev/null +++ b/weasyprint.ipynb @@ -0,0 +1,283 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Weasyprint" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from weasyprint import HTML, CSS\n", + "from weasyprint.fonts import FontConfiguration" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "https://weasyprint.readthedocs.io/en/latest/tutorial.html" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# If you use @font-face in your stylesheet, you would need Weasyprint's FontConfiguration()\n", + "font_config = FontConfiguration()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## HTML" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# small example HTML object\n", + "html = HTML(string='

hello

')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "or ..." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# making an HTML object using our mini-datasets\n", + "import json\n", + "\n", + "f = open('json-dataset.json').read()\n", + "dataset = json.loads(f)\n", + "print(dataset)\n", + "\n", + "content = ''\n", + "\n", + "for word, value in dataset.items():\n", + " content += f'{ word } ({ value })
'\n", + " \n", + "html = HTML(string=content)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "or ..." + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [], + "source": [ + "# making an HTML object using our mini-datasets to insert a layer into a text\n", + "import json, nltk\n", + "\n", + "f = open('json-dataset.json').read()\n", + "dataset = json.loads(f)\n", + "#print(dataset)\n", + "\n", + "txt = open('txt/language.txt').read()\n", + "words = nltk.word_tokenize(txt)\n", + "#print(words)\n", + "\n", + "content = ''\n", + "\n", + "content += '

Language and Software Studies, by Florian Cramer

'\n", + "\n", + "for word in words:\n", + " if word in dataset:\n", + " content += f'{ word } ({ value }) '\n", + " else:\n", + " content += f' { word } '\n", + "\n", + "html = HTML(string=content)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## CSS" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": {}, + "outputs": [], + "source": [ + "css = CSS(string='''\n", + " @page{\n", + " size: A4;\n", + " margin: 15mm;\n", + " background-color: lightgrey;\n", + " font-family: monospace;\n", + " font-size: 8pt;\n", + " color: red;\n", + " border:1px dotted red;\n", + " \n", + " @top-left{\n", + " content: \"natural\";\n", + " }\n", + " @top-center{\n", + " content: \"language\";\n", + " }\n", + " @top-right{\n", + " content: \"artificial\";\n", + " }\n", + " @top-middle{\n", + " content: \"\"\n", + " }\n", + " @left-top{\n", + " content: \"computer control\";\n", + " }\n", + " @right-top{\n", + " content: \"markup\";\n", + " }\n", + " @bottom-left{\n", + " content: \"formal\";\n", + " }\n", + " @bottom-center{\n", + " content: \"programming\";\n", + " }\n", + " @bottom-right{\n", + " content: \"machine\";\n", + " }\n", + " }\n", + " body{\n", + " font-family: serif;\n", + " font-size: 12pt;\n", + " line-height: 1.4;\n", + " color: magenta;\n", + " }\n", + " h1{\n", + " width: 100%;\n", + " text-align: center;\n", + " font-size: 250%;\n", + " line-height: 1.25;\n", + " color: orange;\n", + " }\n", + " strong{\n", + " color: blue;\n", + " }\n", + " em{\n", + " color: green;\n", + " }\n", + "''', font_config=font_config)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## PDF" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "metadata": {}, + "outputs": [], + "source": [ + "html.write_pdf('weasyprint-test.pdf', stylesheets=[css], font_config=font_config)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Previewing the PDF" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "execution_count": 117, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from IPython.display import IFrame\n", + "IFrame(\"weasyprint-test.pdf\", width=1024, height=600)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.3" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}