mirror of
https://github.com/SinTan1729/random.git
synced 2024-12-26 05:08:36 -06:00
new: Added normalize-pdf-pagewidth.py
This commit is contained in:
parent
b268b707bc
commit
0276bb18b2
2 changed files with 49 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
|||
![Number of scripts](https://img.shields.io/badge/number_of_scripts-35-blue)
|
||||
![Number of scripts](https://img.shields.io/badge/number_of_scripts-36-blue)
|
||||
# Random Scripts
|
||||
This repository is for random scripts I wrote mostly for personal use.
|
||||
|
||||
|
|
48
normalize-pdf-pagewidth.py
Normal file
48
normalize-pdf-pagewidth.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
#!/bin/env python3
|
||||
|
||||
# This script resized all pages of a given pdf to make them all the same width.
|
||||
# It can be set up to make them all the same as the min width of the given pdf,
|
||||
# or the max width, or a custom width.
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from pypdf import PdfReader, PdfWriter
|
||||
from pathlib import Path
|
||||
|
||||
parser = ArgumentParser(
|
||||
prog = 'Normalize PDF pagewidth',
|
||||
description = 'A small program that does exactly what it says',
|
||||
epilog = 'For each input file \'input.pdf\', the output file will be named \'input-norm.pdf\''
|
||||
)
|
||||
|
||||
parser.add_argument('filename', help = 'The name of the input file. Multiple files can be supplied at once.', nargs = '+')
|
||||
parser.add_argument('-w', '--width', default = 'min', help = 'The desired width. Can be \'max\', \'min\', or a number in pixels. Defaults to min.')
|
||||
args = vars(parser.parse_args())
|
||||
|
||||
w = args['width']
|
||||
if w != 'min' and w != 'max' and not w.isnumeric():
|
||||
print('Invalid width. Only min, max or numeric values are supported.')
|
||||
exit(-1)
|
||||
|
||||
for file in args['filename']:
|
||||
print('Processing ' + file + '...')
|
||||
try:
|
||||
reader = PdfReader(file)
|
||||
except:
|
||||
print('There was some error trying to read the file.')
|
||||
else:
|
||||
pages = reader.pages
|
||||
outfile = Path(file).stem + '-norm.pdf'
|
||||
if w == 'min':
|
||||
outw = min([pages[i].mediabox.width for i in range(len(pages))])
|
||||
elif w == 'max':
|
||||
outw = max([pages[i].mediabox.width for i in range(len(pages))])
|
||||
else:
|
||||
outw = w
|
||||
writer = PdfWriter()
|
||||
for page in pages:
|
||||
dim = page.mediabox
|
||||
outh = dim.height * (outw / dim.width)
|
||||
page.scale_to(width = outw, height = outh)
|
||||
writer.add_page(page)
|
||||
with open(outfile, 'wb+') as f:
|
||||
writer.write(f)
|
Loading…
Reference in a new issue