updating the ASCII and reportlab canvas notebooks + reportlab cheatsheet, clearing all outputs

master
manetta 4 years ago
parent fa7d27fbd3
commit a47c04dfc5

@ -16,7 +16,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 81, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -26,7 +26,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 87, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -35,81 +35,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 88, "execution_count": null,
"metadata": {}, "metadata": {
"outputs": [ "scrolled": true
{ },
"name": "stdout", "outputs": [],
"output_type": "stream",
"text": [
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
]
}
],
"source": [ "source": [
"# Simple fill-up of a page, to set the reach of our canvas\n", "# Simple fill-up of a page, to set the reach of our canvas\n",
"lines = [] # All the lines will be stored here\n", "lines = [] # All the lines will be stored here\n",
@ -129,7 +59,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# Another variation\n", "# Another way to fill-up the page\n",
"import random \n", "import random \n",
"\n", "\n",
"lines = []\n", "lines = []\n",
@ -149,14 +79,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 90, "execution_count": null,
"metadata": {}, "metadata": {
"scrolled": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# Same variation, but with multiple pages\n", "# Same as above + a \"switch\" to make multipaged PDFs\n",
"import random \n", "import random \n",
"\n", "\n",
"multipage = True\n", "multipage = True\n",
"number_of_pages = 100\n",
"\n", "\n",
"lines = []\n", "lines = []\n",
"pages = []\n", "pages = []\n",
@ -164,7 +97,7 @@
"txt = open('txt/language.txt', 'r').read()\n", "txt = open('txt/language.txt', 'r').read()\n",
"words = txt.split()\n", "words = txt.split()\n",
"\n", "\n",
"for page in range(100):\n", "for page in range(number_of_pages):\n",
" \n", " \n",
" for linenumber in range(height):\n", " for linenumber in range(height):\n",
" word = random.choice(words)\n", " word = random.choice(words)\n",
@ -176,7 +109,10 @@
" # Add a page\n", " # Add a page\n",
" pages.append(lines)\n", " pages.append(lines)\n",
" lines = []\n", " lines = []\n",
" " "\n",
"for page in pages:\n",
" print('\\n'.join(page))\n",
" print('-' * width)"
] ]
}, },
{ {
@ -209,7 +145,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 91, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -220,7 +156,7 @@
"pagewidth = 210*mm\n", "pagewidth = 210*mm\n",
"pageheight = 297*mm\n", "pageheight = 297*mm\n",
"\n", "\n",
"c = Canvas(\"pdf/ASCII-canvas-to-PDF.pdf\", pagesize=(pagewidth, pageheight), bottomup=0)\n", "c = Canvas(\"ASCII-canvas-to-PDF.pdf\", pagesize=(pagewidth, pageheight), bottomup=0)\n",
"c.setFont('Courier', 12)\n", "c.setFont('Courier', 12)\n",
"\n", "\n",
"start_y = 10*mm # start position of the lines\n", "start_y = 10*mm # start position of the lines\n",
@ -242,13 +178,6 @@
" \n", " \n",
"c.save()" "c.save()"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {

@ -1,29 +1,45 @@
{ {
"cells": [ "cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Reportlab Canvas (A4) - Bag of Words"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An example notebook to make PDFs with Reportlab. \n",
"\n",
"This notebook draws 100 words on an A4 PDF."
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 266, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from reportlab.pdfgen.canvas import Canvas\n", "from reportlab.pdfgen.canvas import Canvas\n",
"from reportlab.lib.pagesizes import A4\n", "from reportlab.lib.pagesizes import A4\n",
"from reportlab.lib.units import mm\n", "from reportlab.lib.units import mm\n",
"from reportlab.lib.colors import pink, magenta, red, blue, yellow, lightyellow" "from reportlab.lib.colors import magenta, lightgrey"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 277, "execution_count": 40,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"c = Canvas(\"pdf/reportlab-canvas-A4-bag-of-words.pdf\", pagesize=(210*mm, 297*mm), bottomup=0)" "c = Canvas(\"reportlab-canvas-A4-bag-of-words.pdf\", pagesize=(210*mm, 297*mm), bottomup=0)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 268, "execution_count": 41,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -32,31 +48,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 269, "execution_count": 42,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# Draw a background color\n", "# Draw a background color\n",
"c.setFillColor(lightyellow)\n", "c.setFillColor(lightgrey)\n",
"c.rect(0, 0, A4[0], A4[1], stroke=0, fill=1)" "c.rect(0, 0, A4[0], A4[1], stroke=0, fill=1)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 270, "execution_count": 43,
"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": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -73,124 +76,166 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 275, "execution_count": null,
"metadata": {}, "metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# First bag-of-words: 100 randomly picked and positioned words, based on the text *Language and Software Studies* (`txt/language.txt`).\n",
"# Use the random module and open the language.txt file \n",
"import random\n",
"\n",
"txt = open('txt/language.txt', 'r').read()\n",
"words = txt.split()\n",
"\n",
"# Draw a random bag-of-words 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)\n",
"\n",
"# Add a small caption\n",
"c.setFont('Courier', 10)\n",
"c.setFillColor(blue)\n",
"c.drawCentredString(105*mm, 290*mm, 'Random words from Language and Software Studies, by Florian Cramer (2005)')\n",
"\n",
"# Save the PDF!\n",
"c.save()"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"scrolled": true
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"37 107 a\n", "84 58 computing\n",
"24 100 formal\n", "173 112 programming\n",
"126 264 a\n", "113 255 processing\n",
"58 279 controls\n", "102 115 programming\n",
"41 250 an\n", "145 38 nothing\n",
"36 142 program\n", "54 71 programming\n",
"111 153 programming\n", "106 139 programming\n",
"32 228 be\n", "107 10 programming\n",
"71 166 Originally\n", "49 84 programming\n",
"154 267 operations)\n", "8 188 expressing\n",
"97 60 are\n", "158 63 programming\n",
"187 158 the\n", "124 257 anything\n",
"98 205 an\n", "39 8 understanding\n",
"169 229 “code”\n", "121 221 programming\n",
"175 144 analog\n", "91 127 programming\n",
"166 9 yet\n", "12 170 understanding\n",
"82 182 the\n", "71 47 programming\n",
"136 34 a\n", "17 33 nothing\n",
"35 268 7\n", "36 280 programming\n",
"13 21 of\n", "175 112 typing\n",
"152 254 of\n", "173 275 thinking\n",
"81 157 call\n", "69 217 involving\n",
"160 104 tomorrow\n", "29 274 meaning\n",
"141 47 promises\n", "122 223 meaning\n",
"140 220 of\n", "75 45 meaning\n",
"155 201 The\n", "104 255 programming\n",
"62 59 terms.”\n", "94 172 Turing\n",
"41 246 difficult\n", "48 181 programming\n",
"61 139 of\n", "29 107 programming\n",
"154 279 in\n", "72 179 speaking\n",
"48 279 coded\n", "185 238 programming\n",
"59 88 artists\n", "71 214 Nothing\n",
"146 186 eventual\n", "25 279 programming\n",
"90 26 virus\n", "76 255 thinking\n",
"102 261 act\n", "31 152 programming\n",
"55 265 computer\n", "26 237 denoting\n",
"83 40 reflect\n", "161 56 storing\n",
"126 144 experiments\n", "6 170 transmitting\n",
"99 30 computation\n", "70 38 “programming\n",
"5 221 concrete\n", "5 112 indicating\n",
"96 129 occurs\n", "156 188 giving\n",
"19 89 computes\n", "56 238 passing\n",
"181 34 their\n", "142 37 accepting\n",
"9 57 is\n", "109 92 trading\n",
"157 68 “performative”\n", "163 161 Writing\n",
"19 95 computer\n", "1 163 programming\n",
"30 44 in\n", "157 1 phrasing\n",
"88 114 instructions\n", "93 161 layering\n",
"65 188 computer\n", "61 170 nothing\n",
"81 149 German\n", "62 162 processing\n",
"70 207 whatever\n", "152 19 string\n",
"113 31 representation\n", "47 259 computing\n",
"166 82 and\n", "63 69 reshaping\n",
"15 55 to\n", "71 144 data—including\n",
"57 163 and\n", "187 156 writing\n",
"118 196 is\n", "156 227 consisting\n",
"51 223 as\n", "125 119 meaning\n",
"67 46 a\n", "68 45 writing\n",
"38 278 handle\n", "123 18 dragging\n",
"136 134 Yet\n", "72 153 encoding\n",
"35 170 poetry\n", "53 209 expressing\n",
"146 111 “Interface”)\n", "31 273 flying\n",
"158 132 suitable\n", "17 32 programming\n",
"187 189 example\n", "166 140 programming\n",
"159 200 would\n", "167 117 programming\n",
"33 241 that\n", "42 214 programming\n",
"178 213 nothing\n", "144 271 encoding\n",
"62 78 critically\n", "131 255 understanding\n",
"166 205 of\n", "133 2 programming\n",
"169 42 more\n", "7 66 programming\n",
"122 154 itself\n", "124 206 involving\n",
"187 278 formal\n", "166 46 writing\n",
"85 59 ”Chapter\n", "158 135 thinking\n",
"35 104 themselves\n", "104 237 understanding\n",
"145 64 states\n", "18 206 amusing\n",
"37 85 or\n", "11 97 according\n",
"81 226 that\n", "9 169 Designing\n",
"19 253 \n", "28 199 According\n",
"157 55 string\n", "152 205 writing\n",
"187 125 semantic\n", "97 274 surprising\n",
"142 224 transformation\n", "49 92 Reading\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": [ "source": [
"# Draw on the canvas\n", "# Another bag-of-words: word selections from Words of the Future\n",
"for n in range(100):\n", "import os\n",
"import random\n",
"\n",
"bag = []\n",
"filenames = []\n",
"\n",
"folder = 'txt/words-for-the-future/'\n",
"for file in os.listdir(folder):\n",
" # print(folder+file)\n",
" txt = open('txt/language.txt', 'r').read()\n",
" words = txt.split()\n",
" bag += words\n",
" filenames.append(file) # We will use this for the caption\n",
"\n",
"# print(bag)\n",
"\n",
"# Select all the words that end on \"ing\" and draw them on the canvas\n",
"for word in words:\n",
" word = word.strip(',/\\\\!?;:\"\\'.') # \"clean up\" the words\n",
" x = random.randint(0, 190)\n", " x = random.randint(0, 190)\n",
" y = random.randint(0, 280)\n", " y = random.randint(0, 280)\n",
" word = random.choice(words).strip(',.')\n", " if word.endswith('ing'):\n",
" print(x, y, word)\n", " print(x, y, word)\n",
" c.drawString(x*mm, y*mm, word)" " c.drawString(x*mm, y*mm, word)\n",
"\n",
"# Add a small caption\n",
"c.setFont('Courier', 10)\n",
"c.setFillColor(blue)\n",
"filenames_str = ', '.join(filenames)\n",
"c.drawCentredString(105*mm, 290*mm, 'Random words from: {f} (Words for the Future)'.format(f=filenames_str))\n",
"\n",
"# Save the PDF!\n",
"c.save()"
] ]
}, },
{ {
@ -221,50 +266,6 @@
"outputs": [], "outputs": [],
"source": [] "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", "cell_type": "code",
"execution_count": null, "execution_count": null,

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