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.3 KiB
7.3 KiB
Reportlab Canvas (A4) - Bag of Words¶
An example notebook to make PDFs with Reportlab.
This notebook draws 100 words on an A4 PDF.
In [ ]:
from reportlab.pdfgen.canvas import Canvas from reportlab.lib.pagesizes import A4 from reportlab.lib.units import mm from reportlab.lib.colors import magenta, lightgrey
In [40]:
c = Canvas("reportlab-canvas-A4-bag-of-words.pdf", pagesize=(210*mm, 297*mm), bottomup=0)
In [41]:
c.setPageSize(A4)
In [42]:
# Draw a background color c.setFillColor(lightgrey) c.rect(0, 0, A4[0], A4[1], stroke=0, fill=1)
In [43]:
# Set a font, change the font color from reportlab.pdfbase.ttfonts import TTFont, pdfmetrics fontpath = "fonts/OSP-DIN.ttf" font = TTFont('OSP-DIN', fontpath) pdfmetrics.registerFont(font) c.setFont('OSP-DIN', 32) c.setFillColor(magenta)
In [ ]:
# First bag-of-words: 100 randomly picked and positioned words, based on the text *Language and Software Studies* (`txt/language.txt`). # Use the random module and open the language.txt file import random txt = open('txt/language.txt', 'r').read() words = txt.split() # Draw a random bag-of-words on the canvas for n in range(100): x = random.randint(0, 190) y = random.randint(0, 280) word = random.choice(words).strip(',.') print(x, y, word) c.drawString(x*mm, y*mm, word) # Add a small caption c.setFont('Courier', 10) c.setFillColor(blue) c.drawCentredString(105*mm, 290*mm, 'Random words from Language and Software Studies, by Florian Cramer (2005)') # Save the PDF! c.save()
In [44]:
# Another bag-of-words: word selections from Words of the Future import os import random bag = [] filenames = [] folder = 'txt/words-for-the-future/' for file in os.listdir(folder): # print(folder+file) txt = open('txt/language.txt', 'r').read() words = txt.split() bag += words filenames.append(file) # We will use this for the caption # print(bag) # Select all the words that end on "ing" and draw them on the canvas for word in words: word = word.strip(',/\\!?;:"\'.') # "clean up" the words x = random.randint(0, 190) y = random.randint(0, 280) if word.endswith('ing'): print(x, y, word) c.drawString(x*mm, y*mm, word) # Add a small caption c.setFont('Courier', 10) c.setFillColor(blue) filenames_str = ', '.join(filenames) c.drawCentredString(105*mm, 290*mm, 'Random words from: {f} (Words for the Future)'.format(f=filenames_str)) # Save the PDF! c.save()
84 58 computing 173 112 programming 113 255 processing 102 115 programming 145 38 nothing 54 71 programming 106 139 programming 107 10 programming 49 84 programming 8 188 expressing 158 63 programming 124 257 anything 39 8 understanding 121 221 programming 91 127 programming 12 170 understanding 71 47 programming 17 33 nothing 36 280 programming 175 112 typing 173 275 thinking 69 217 involving 29 274 meaning 122 223 meaning 75 45 meaning 104 255 programming 94 172 Turing 48 181 programming 29 107 programming 72 179 speaking 185 238 programming 71 214 Nothing 25 279 programming 76 255 thinking 31 152 programming 26 237 denoting 161 56 storing 6 170 transmitting 70 38 “programming 5 112 indicating 156 188 giving 56 238 passing 142 37 accepting 109 92 trading 163 161 Writing 1 163 programming 157 1 phrasing 93 161 layering 61 170 nothing 62 162 processing 152 19 string 47 259 computing 63 69 reshaping 71 144 data—including 187 156 writing 156 227 consisting 125 119 meaning 68 45 writing 123 18 dragging 72 153 encoding 53 209 expressing 31 273 flying 17 32 programming 166 140 programming 167 117 programming 42 214 programming 144 271 encoding 131 255 understanding 133 2 programming 7 66 programming 124 206 involving 166 46 writing 158 135 thinking 104 237 understanding 18 206 amusing 11 97 according 9 169 Designing 28 199 According 152 205 writing 97 274 surprising 49 92 Reading
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: