-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_double_sided_scans.py
59 lines (47 loc) · 2.42 KB
/
process_double_sided_scans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Proceses batch scanned double-sided PDFs into individual files
This script takes PDFs that are scanned in two parts (odd pages and even pages) with a single-sided
document feeder. It is presumed that the even pages are scanned in reverse order (i.e.
back-to-front). The odd and even pages are interleaved into a coherent document, with the even pages
placed in the correct order. Individual documents are then ouput to separate files.
"""
from PyPDF2 import PdfReader, PdfWriter
INPUT_FILE_ODD = str(input(
'Enter the input file for odd pages (default = odd.pdf): ') or 'odd.pdf')
INPUT_FILE_EVEN = str(input(
'Enter the input file for odd pages (default = even.pdf): ') or 'even.pdf')
PAGES_PER_DOCUMENT = int(input(
'Pages per document (i.e. split every n pages) (default = 2): ') or '2')
with open(INPUT_FILE_ODD, 'rb') as input_odd_read_file:
input_odd = PdfReader(input_odd_read_file)
total_pages_odd = len(input_odd.pages)
with open(INPUT_FILE_EVEN, 'rb') as input_even_read_file:
input_even = PdfReader(input_even_read_file)
total_pages_even = len(input_even.pages)
alternate_mix = PdfWriter()
for page in range(total_pages_odd):
alternate_mix.add_page(input_odd.pages[page])
if total_pages_odd == total_pages_even:
alternate_mix.add_page(
input_even.pages[total_pages_odd - page - 1])
elif total_pages_odd == total_pages_even + 1:
alternate_mix.add_page(
input_even.pages[total_pages_odd - page - 2])
else:
print('Error')
for document in range(-((total_pages_odd + total_pages_even) // -PAGES_PER_DOCUMENT)):
output = PdfWriter()
if (PAGES_PER_DOCUMENT % 2) == 0:
for page in range(PAGES_PER_DOCUMENT):
output.add_page(
alternate_mix.pages[document * PAGES_PER_DOCUMENT + page])
else:
try:
for page in range(PAGES_PER_DOCUMENT):
output.add_page(
alternate_mix.pages[document * (PAGES_PER_DOCUMENT + 1) + page])
except:
break
OUTPUT_NAME = 'output_' + str(document + 1) + '.pdf'
with open(OUTPUT_NAME, 'wb') as write_file:
output.write(write_file)
print('Created ' + OUTPUT_NAME)