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.
4.4 KiB
4.4 KiB
ASCII Canvas¶
(using Reportlab to export it as a PDF)
In [ ]:
width = 75 height = 65
In [ ]:
multipage = False
In [ ]:
# Simple fill-up of a page, to set the reach of our canvas lines = [] # All the lines will be stored here for linenumber in range(height): line = 'x' * width lines.append(line) print('\n'.join(lines))
In [ ]:
# Another way to fill-up the page import random lines = [] txt = open('txt/language.txt', 'r').read() words = txt.split() for linenumber in range(height): word = random.choice(words) length_of_word = len(word) nr_of_words_fit_in_line = width / length_of_word line = word * int(nr_of_words_fit_in_line) lines.append(line) print('\n'.join(lines))
In [ ]:
# Same as above + a "switch" to make multipaged PDFs import random multipage = True number_of_pages = 100 lines = [] pages = [] txt = open('txt/language.txt', 'r').read() words = txt.split() for page in range(number_of_pages): for linenumber in range(height): word = random.choice(words) length_of_word = len(word) nr_of_words_fit_in_line = width / length_of_word line = word * int(nr_of_words_fit_in_line) lines.append(line) # Add a page pages.append(lines) lines = [] for page in pages: print('\n'.join(page)) print('-' * width)
In [ ]:
In [ ]:
In [ ]:
Exporting with Reportlab¶
In [ ]:
from reportlab.pdfgen.canvas import Canvas from reportlab.lib.pagesizes import A4 from reportlab.lib.units import mm pagewidth = 210*mm pageheight = 297*mm c = Canvas("ASCII-canvas-to-PDF.pdf", pagesize=(pagewidth, pageheight), bottomup=0) c.setFont('Courier', 12) start_y = 10*mm # start position of the lines y = start_y lineheight = 4*mm if multipage == True: for page in pages: for line in page: c.drawCentredString(pagewidth/2, y, line) y += lineheight c.showPage() y = start_y else: for line in lines: c.drawCentredString(pagewidth/2, y, line) y += lineheight c.save()