adding two notebooks with reportlab & ASCII canvas examples, and also adding a reportlab-cheatsheet in the making :--)

master
manetta 4 years ago
parent b7e495787e
commit feea70494a

@ -0,0 +1,275 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ASCII Canvas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(using Reportlab to export it as a PDF)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
"width = 75\n",
"height = 65"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
"multipage = False"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
]
}
],
"source": [
"# Simple fill-up of a page, to set the reach of our canvas\n",
"lines = [] # All the lines will be stored here\n",
"\n",
"for linenumber in range(height):\n",
" line = 'x' * width\n",
" lines.append(line)\n",
"\n",
"print('\\n'.join(lines))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Another variation\n",
"import random \n",
"\n",
"lines = []\n",
"\n",
"txt = open('txt/language.txt', 'r').read()\n",
"words = txt.split()\n",
"\n",
"for linenumber in range(height):\n",
" word = random.choice(words)\n",
" length_of_word = len(word)\n",
" nr_of_words_fit_in_line = width / length_of_word\n",
" line = word * int(nr_of_words_fit_in_line)\n",
" lines.append(line)\n",
" \n",
"print('\\n'.join(lines))"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [],
"source": [
"# Same variation, but with multiple pages\n",
"import random \n",
"\n",
"multipage = True\n",
"\n",
"lines = []\n",
"pages = []\n",
"\n",
"txt = open('txt/language.txt', 'r').read()\n",
"words = txt.split()\n",
"\n",
"for page in range(100):\n",
" \n",
" for linenumber in range(height):\n",
" word = random.choice(words)\n",
" length_of_word = len(word)\n",
" nr_of_words_fit_in_line = width / length_of_word\n",
" line = word * int(nr_of_words_fit_in_line)\n",
" lines.append(line)\n",
" \n",
" # Add a page\n",
" pages.append(lines)\n",
" lines = []\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exporting with Reportlab"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [],
"source": [
"from reportlab.pdfgen.canvas import Canvas\n",
"from reportlab.lib.pagesizes import A4\n",
"from reportlab.lib.units import mm\n",
"\n",
"pagewidth = 210*mm\n",
"pageheight = 297*mm\n",
"\n",
"c = Canvas(\"pdf/ASCII-canvas-to-PDF.pdf\", pagesize=(pagewidth, pageheight), bottomup=0)\n",
"c.setFont('Courier', 12)\n",
"\n",
"start_y = 10*mm # start position of the lines\n",
"\n",
"y = start_y\n",
"lineheight = 4*mm\n",
"\n",
"if multipage == True:\n",
" for page in pages:\n",
" for line in page:\n",
" c.drawCentredString(pagewidth/2, y, line)\n",
" y += lineheight\n",
" c.showPage()\n",
" y = start_y\n",
"else:\n",
" for line in lines:\n",
" c.drawCentredString(pagewidth/2, y, line)\n",
" y += lineheight\n",
" \n",
"c.save()"
]
},
{
"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
}

@ -0,0 +1,297 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 266,
"metadata": {},
"outputs": [],
"source": [
"from reportlab.pdfgen.canvas import Canvas\n",
"from reportlab.lib.pagesizes import A4\n",
"from reportlab.lib.units import mm\n",
"from reportlab.lib.colors import pink, magenta, red, blue, yellow, lightyellow"
]
},
{
"cell_type": "code",
"execution_count": 277,
"metadata": {},
"outputs": [],
"source": [
"c = Canvas(\"pdf/reportlab-canvas-A4-bag-of-words.pdf\", pagesize=(210*mm, 297*mm), bottomup=0)"
]
},
{
"cell_type": "code",
"execution_count": 268,
"metadata": {},
"outputs": [],
"source": [
"c.setPageSize(A4)"
]
},
{
"cell_type": "code",
"execution_count": 269,
"metadata": {},
"outputs": [],
"source": [
"# Draw a background color\n",
"c.setFillColor(lightyellow)\n",
"c.rect(0, 0, A4[0], A4[1], stroke=0, fill=1)"
]
},
{
"cell_type": "code",
"execution_count": 270,
"metadata": {},
"outputs": [],
"source": [
"# Draw a bag-of-words on the canvas, on random positions\n",
"import random\n",
"\n",
"txt = open('txt/language.txt', 'r').read()\n",
"words = txt.split()"
]
},
{
"cell_type": "code",
"execution_count": 271,
"metadata": {},
"outputs": [],
"source": [
"# Set a font, change the font color\n",
"from reportlab.pdfbase.ttfonts import TTFont, pdfmetrics\n",
"\n",
"fontpath = \"fonts/OSP-DIN.ttf\"\n",
"font = TTFont('OSP-DIN', fontpath)\n",
"pdfmetrics.registerFont(font)\n",
"\n",
"c.setFont('OSP-DIN', 32)\n",
"c.setFillColor(magenta)"
]
},
{
"cell_type": "code",
"execution_count": 275,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"37 107 a\n",
"24 100 formal\n",
"126 264 a\n",
"58 279 controls\n",
"41 250 an\n",
"36 142 program\n",
"111 153 programming\n",
"32 228 be\n",
"71 166 Originally\n",
"154 267 operations)\n",
"97 60 are\n",
"187 158 the\n",
"98 205 an\n",
"169 229 “code”\n",
"175 144 analog\n",
"166 9 yet\n",
"82 182 the\n",
"136 34 a\n",
"35 268 7\n",
"13 21 of\n",
"152 254 of\n",
"81 157 call\n",
"160 104 tomorrow\n",
"141 47 promises\n",
"140 220 of\n",
"155 201 The\n",
"62 59 terms.”\n",
"41 246 difficult\n",
"61 139 of\n",
"154 279 in\n",
"48 279 coded\n",
"59 88 artists\n",
"146 186 eventual\n",
"90 26 virus\n",
"102 261 act\n",
"55 265 computer\n",
"83 40 reflect\n",
"126 144 experiments\n",
"99 30 computation\n",
"5 221 concrete\n",
"96 129 occurs\n",
"19 89 computes\n",
"181 34 their\n",
"9 57 is\n",
"157 68 “performative”\n",
"19 95 computer\n",
"30 44 in\n",
"88 114 instructions\n",
"65 188 computer\n",
"81 149 German\n",
"70 207 whatever\n",
"113 31 representation\n",
"166 82 and\n",
"15 55 to\n",
"57 163 and\n",
"118 196 is\n",
"51 223 as\n",
"67 46 a\n",
"38 278 handle\n",
"136 134 Yet\n",
"35 170 poetry\n",
"146 111 “Interface”)\n",
"158 132 suitable\n",
"187 189 example\n",
"159 200 would\n",
"33 241 that\n",
"178 213 nothing\n",
"62 78 critically\n",
"166 205 of\n",
"169 42 more\n",
"122 154 itself\n",
"187 278 formal\n",
"85 59 ”Chapter\n",
"35 104 themselves\n",
"145 64 states\n",
"37 85 or\n",
"81 226 that\n",
"19 253 \n",
"157 55 string\n",
"187 125 semantic\n",
"142 224 transformation\n",
"115 3 the\n",
"98 46 Wesley\n",
"61 202 a\n",
"178 32 in\n",
"110 211 command\n",
"169 240 be\n",
"166 82 symbols\n",
"34 192 languages\n",
"30 68 is\n",
"46 172 that\n",
"3 276 “natural\n",
"123 242 computer\n",
"100 123 common\n",
"162 248 that\n",
"140 31 it\n",
"9 146 are\n",
"165 10 in\n",
"132 271 control\n",
"173 276 But\n"
]
}
],
"source": [
"# Draw on the canvas\n",
"for n in range(100):\n",
" x = random.randint(0, 190)\n",
" y = random.randint(0, 280)\n",
" word = random.choice(words).strip(',.')\n",
" print(x, y, word)\n",
" c.drawString(x*mm, y*mm, word)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 273,
"metadata": {},
"outputs": [],
"source": [
"# Add a small caption\n",
"c.setFont('Courier', 10)\n",
"c.setFillColor(blue)\n",
"c.drawCentredString(105*mm, 285*mm, 'Random words from Language and Software Studies, by Florian Cramer (2005)')"
]
},
{
"cell_type": "code",
"execution_count": 274,
"metadata": {},
"outputs": [],
"source": [
"# Save the PDF!\n",
"#c.showPage()\n",
"c.save()"
]
},
{
"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
}

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save