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.
205 lines
4.4 KiB
Plaintext
205 lines
4.4 KiB
Plaintext
{
|
|
"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": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"width = 75\n",
|
|
"height = 65"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"multipage = False"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"scrolled": true
|
|
},
|
|
"outputs": [],
|
|
"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 way to fill-up the page\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": null,
|
|
"metadata": {
|
|
"scrolled": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Same as above + a \"switch\" to make multipaged PDFs\n",
|
|
"import random \n",
|
|
"\n",
|
|
"multipage = True\n",
|
|
"number_of_pages = 100\n",
|
|
"\n",
|
|
"lines = []\n",
|
|
"pages = []\n",
|
|
"\n",
|
|
"txt = open('txt/language.txt', 'r').read()\n",
|
|
"words = txt.split()\n",
|
|
"\n",
|
|
"for page in range(number_of_pages):\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",
|
|
"\n",
|
|
"for page in pages:\n",
|
|
" print('\\n'.join(page))\n",
|
|
" print('-' * width)"
|
|
]
|
|
},
|
|
{
|
|
"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": 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",
|
|
"\n",
|
|
"pagewidth = 210*mm\n",
|
|
"pageheight = 297*mm\n",
|
|
"\n",
|
|
"c = Canvas(\"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()"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|