Difference between revisions of "Make library card catalogue PDFs with Python scripts"

From Parallel Library Services
Jump to navigation Jump to search
Line 41: Line 41:


This will produce a PDF, and also a list of the contents. In my case, it produced a 1048 page PDF in seconds, with the title and author of each book on separate pages of a card catalogue.
This will produce a PDF, and also a list of the contents. In my case, it produced a 1048 page PDF in seconds, with the title and author of each book on separate pages of a card catalogue.
=== How it works ===
The python script <code>reportlab_image_poster.py</code>, depends on <code>readfrompad.py</code>.
reportlab_image_poster.py
<syntaxhighlight lang="python" line>
#!/usr/bin/env python3
import os, datetime, sys
from argparse import ArgumentParser
from glob import glob
from PIL import Image
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import *
from calibrestekje import Book, Publisher, init_session
# p = ArgumentParser("")
# p.add_argument("--output", default="poster.pdf")
# p.add_argument("--interpolation", default="cubic", help="nearest,cubic")
# p.add_argument("--labels", default="labels_public.txt")
# args = p.parse_args()
pagewidth, pageheight = landscape(A6)
c = canvas.Canvas("card_catalogue.pdf", pagesize=landscape(A6))
# x, y = 0, 0
# imagewidth = 200
# aw = pagewidth - imagewidth
# images = (glob ("images/*.JPG"))
# dx = aw/(len(images)-1)
# dy = 20
session = init_session("sqlite:///metadata.db")
# publisher = (session.query(Publisher)
#                    .filter(Publisher.name == "MIT Press").one())
for book in session.query(Book).all():
    print (book.title)
    c.drawString(10,pageheight-10, book.title)
    c.showPage()
    # print (image)
    # im = Image.open(image)
    # pxwidth, pxheight = im.size
    # print ("Got the image, it's size is:", im.size)
    # imageheight = imagewidth * (pxheight / pxwidth)
    # c.drawInlineImage(image, x, y, imagewidth, None)
    # print ("placing image {0} at {1}".format(image, (x,y)))
    # x += dx
    # y += dy
c.save()
# sys.exit(0)
#################
# GRID
# imsize = 96
# cols = int(A0[0] // imsize)
# rows = int(A0[1] // imsize)
# # calculate margins to center the grid on the page
# mx = (A0[0] - (cols*imsize)) / 2
# my = (A0[1] - (rows*imsize)) / 2
# print ("Grid size {0}x{1} (cols x rows)".format(cols, rows))
# print ("  (total size:", cols*imsize, rows*imsize, "margins:", mx, my, ")")
#################
</syntaxhighlight>
[[Category: Cookbook]]
[[Category: Cookbook]]

Revision as of 17:55, 20 October 2021

This recipe depends on

  • a python virtual environment
  • a cloned git repository
  • calibrestekje, a python-bindings library
  • a metadata.db file as produced by an existing installation of Calibre

Calibrestkje works with an existing Calibre database. This means, a previously installed version of Calibre. If you don't have calibre installed yet, you should do this before trying out this recipe. The contents that are produced in the PDF depend entirely on what is in the metadata.db file. Alternatively, it may also be useful to install calibre and force it to make an empty, but valid file which can be written to using calibredb, a tool that comes with the calibre package. There is a handy guide for how to work with the Calibre database on the examples page for Calibrestekje.

Getting started

First, clone the git and change to the new bootleg/ directory:

git clone https://git.xpub.nl/simoon/bootleg.git
cd bootleg/

In the bootleg/ directory, create and activate a python virtual environment. Once activated, you'll notice the prompt in the terminal has changed to be prefaced by (venv), which indicates that the virtual environment is active.

python3 -m venv venv
source venv/bin/activate

Then, install dependencies in the python environment.

pip install reportlab 
pip install calibrestekje
pip install pillow

Make sure you have a valid metadata.db file in the same bootleg/ directory. One which is usually produced the first time you run Calibre in a path similar to /home/myusername/calibre/metadata.db on Debian and Unix-like systems. This file is usually kept with the contents of the Calibre book collection.

Next, run this command:

python3 reportlab_image_poster.py

This will produce a PDF, and also a list of the contents. In my case, it produced a 1048 page PDF in seconds, with the title and author of each book on separate pages of a card catalogue.

How it works

The python script reportlab_image_poster.py, depends on readfrompad.py.

reportlab_image_poster.py

#!/usr/bin/env python3

import os, datetime, sys
from argparse import ArgumentParser
from glob import glob

from PIL import Image
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import *
from calibrestekje import Book, Publisher, init_session

# p = ArgumentParser("")
# p.add_argument("--output", default="poster.pdf")
# p.add_argument("--interpolation", default="cubic", help="nearest,cubic")
# p.add_argument("--labels", default="labels_public.txt")
# args = p.parse_args()


pagewidth, pageheight = landscape(A6)

c = canvas.Canvas("card_catalogue.pdf", pagesize=landscape(A6))
# x, y = 0, 0
# imagewidth = 200
# aw = pagewidth - imagewidth
# images = (glob ("images/*.JPG"))
# dx = aw/(len(images)-1)
# dy = 20

session = init_session("sqlite:///metadata.db")

# publisher = (session.query(Publisher)
#                     .filter(Publisher.name == "MIT Press").one())
for book in session.query(Book).all():


    print (book.title)
    c.drawString(10,pageheight-10, book.title)
    c.showPage()
    # print (image)
    # im = Image.open(image)
    # pxwidth, pxheight = im.size
    # print ("Got the image, it's size is:", im.size)
    # imageheight = imagewidth * (pxheight / pxwidth)
    # c.drawInlineImage(image, x, y, imagewidth, None)
    # print ("placing image {0} at {1}".format(image, (x,y)))
    # x += dx
    # y += dy

c.save()
# sys.exit(0)


#################
# GRID
# imsize = 96
# cols = int(A0[0] // imsize)
# rows = int(A0[1] // imsize)
# # calculate margins to center the grid on the page
# mx = (A0[0] - (cols*imsize)) / 2
# my = (A0[1] - (rows*imsize)) / 2
# print ("Grid size {0}x{1} (cols x rows)".format(cols, rows))
# print ("  (total size:", cols*imsize, rows*imsize, "margins:", mx, my, ")")
#################