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.

299 lines
7.3 KiB
Plaintext

{
"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",
"execution_count": null,
"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 magenta, lightgrey"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"c = Canvas(\"reportlab-canvas-A4-bag-of-words.pdf\", pagesize=(210*mm, 297*mm), bottomup=0)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"c.setPageSize(A4)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"# Draw a background color\n",
"c.setFillColor(lightgrey)\n",
"c.rect(0, 0, A4[0], A4[1], stroke=0, fill=1)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"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": null,
"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": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"84 58 computing\n",
"173 112 programming\n",
"113 255 processing\n",
"102 115 programming\n",
"145 38 nothing\n",
"54 71 programming\n",
"106 139 programming\n",
"107 10 programming\n",
"49 84 programming\n",
"8 188 expressing\n",
"158 63 programming\n",
"124 257 anything\n",
"39 8 understanding\n",
"121 221 programming\n",
"91 127 programming\n",
"12 170 understanding\n",
"71 47 programming\n",
"17 33 nothing\n",
"36 280 programming\n",
"175 112 typing\n",
"173 275 thinking\n",
"69 217 involving\n",
"29 274 meaning\n",
"122 223 meaning\n",
"75 45 meaning\n",
"104 255 programming\n",
"94 172 Turing\n",
"48 181 programming\n",
"29 107 programming\n",
"72 179 speaking\n",
"185 238 programming\n",
"71 214 Nothing\n",
"25 279 programming\n",
"76 255 thinking\n",
"31 152 programming\n",
"26 237 denoting\n",
"161 56 storing\n",
"6 170 transmitting\n",
"70 38 “programming\n",
"5 112 indicating\n",
"156 188 giving\n",
"56 238 passing\n",
"142 37 accepting\n",
"109 92 trading\n",
"163 161 Writing\n",
"1 163 programming\n",
"157 1 phrasing\n",
"93 161 layering\n",
"61 170 nothing\n",
"62 162 processing\n",
"152 19 string\n",
"47 259 computing\n",
"63 69 reshaping\n",
"71 144 data—including\n",
"187 156 writing\n",
"156 227 consisting\n",
"125 119 meaning\n",
"68 45 writing\n",
"123 18 dragging\n",
"72 153 encoding\n",
"53 209 expressing\n",
"31 273 flying\n",
"17 32 programming\n",
"166 140 programming\n",
"167 117 programming\n",
"42 214 programming\n",
"144 271 encoding\n",
"131 255 understanding\n",
"133 2 programming\n",
"7 66 programming\n",
"124 206 involving\n",
"166 46 writing\n",
"158 135 thinking\n",
"104 237 understanding\n",
"18 206 amusing\n",
"11 97 according\n",
"9 169 Designing\n",
"28 199 According\n",
"152 205 writing\n",
"97 274 surprising\n",
"49 92 Reading\n"
]
}
],
"source": [
"# Another bag-of-words: word selections from Words of the Future\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",
" y = random.randint(0, 280)\n",
" if word.endswith('ing'):\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",
"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()"
]
},
{
"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": []
}
],
"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
}