1
0
Fork 0
mirror of https://github.com/SinTan1729/random.git synced 2024-12-26 21:18:36 -06:00
random/arrange-docpages-for-print.py

48 lines
2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2023-02-20 21:57:43 -06:00
# This is a script to paste small scanned ID cards in an A4 page for printing
# I use it to quickly organize the pages for printing
# Syntax : script.py filename width_in_cm -o [optional, to overwrite output file]
2023-02-20 21:57:43 -06:00
import sys
import os
from pdf2image import convert_from_path
from PIL import Image
2023-02-20 21:57:43 -06:00
if len(sys.argv) < 3:
raise Exception(
'Not enough arguments.\nUse the syntax "script.py filename width_in_cm -o [optional, to overwrite output file]"')
filename = sys.argv[1]
if not os.path.isfile(filename):
2023-02-20 21:57:43 -06:00
raise Exception('File {} not present', format(filename))
if filename.split('.')[-1] != 'pdf':
raise Exception('Given file is not a PDF')
2023-02-20 21:57:43 -06:00
if filename.split('/')[-1].split(']')[0] == '[printable':
raise Exception('File has already been processed')
print('Processing '+filename.split('/')[-1])
2023-02-20 21:57:43 -06:00
# converting to inch from cm at 300 dpi
new_width = int(int(sys.argv[2])*0.3937*300)
overwrite_status = '-n'
2023-02-20 21:57:43 -06:00
if len(sys.argv) > 3:
overwrite_status = sys.argv[3]
images = convert_from_path(filename)
a4size = int(8.27*300), int(11.7*300)
2023-02-20 21:57:43 -06:00
a4page = Image.new('RGB', a4size, 'white') # A4 at 300 dpi
pagenum = len(images)
widths = [images[i].size[0] for i in range(pagenum)]
heights = [images[i].size[1] for i in range(pagenum)]
new_heights = [int(heights[i]*new_width/widths[i]) for i in range(pagenum)]
gap = int(max(new_heights)/pagenum)
total_height = sum(new_heights)+gap*(pagenum-1)
if new_width > a4size[0] or total_height > a4size[1]:
2023-02-20 21:57:43 -06:00
raise Exception('Given width is too big for A4 page')
for i in range(pagenum):
2023-02-20 21:57:43 -06:00
a4page.paste(images[i].resize((new_width, new_heights[i]), Image.LANCZOS), (int(
(a4size[0]-new_width)/2), int((a4size[1]-total_height)/2+i*(new_heights[i-1]+gap))))
new_filename = '[printable] ' + filename.split('/')[-1].split('.')[0] + '.pdf'
if os.path.isfile(new_filename) and overwrite_status != '-o':
2023-02-20 21:57:43 -06:00
raise Exception('File '+new_filename +
' is already present. Add -o at the end to overwrite')
a4page.save(new_filename, quality=95)
print('File '+new_filename+' created.')