Skip to content

Commit

Permalink
splitted in package and scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLeeh authored and MrLeeh committed Feb 17, 2015
1 parent 3be3252 commit 2d8aa28
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 130 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ pdftools
* **License:** MIT
* **Description:*** This is a small collection of convenience python scripts for fast pdf manipulation via commandline.



## Features

* split PDF files in multiple documents
* merge PDF files into one document
* rotate PDF files
* zip PDF files in one document

## Usage

Expand Down Expand Up @@ -73,9 +72,9 @@ optional arguments:
```

## pdfzip
Zip the pages of two input files in one output file. This when dealing with
scanned documents where even pages are in one docuemnt and odd pages in the
other.
Zip the pages of two input files in one output file. This is useful when
dealing with scanned documents where even pages are in one docuemnt and
odd pages in the other.

```bash
usage: pdfzip.py [-h] -o OUTPUT [-d] input1 input2
Expand Down
37 changes: 2 additions & 35 deletions pdfmerge.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
#! python3

import os
import sys
import argparse
from PyPDF2 import PdfFileReader, PdfFileWriter


def pdf_merge(inputs, output, delete=False):
writer = PdfFileWriter()
if os.path.isfile(output):
ans = input("The file '%s' already exists. "
"Overwrite? Yes/Abort [Y/a]: " % output).lower()
if ans == "a":
return

outputfile = open(output, "wb")

try:
infiles = []
for filename in inputs:
f = open(filename, 'rb')
reader = PdfFileReader(f)
for page in reader.pages:
writer.addPage(page)
infiles.append(f)
writer.write(outputfile)
except FileNotFoundError as e:
print(e.strerror + ": " + e.filename)
finally:
outputfile.close()
for f in infiles:
f.close()

if delete:
for filename in inputs:
os.remove(filename)
from pdftools import pdf_merge


def process_arguments(args):
Expand Down Expand Up @@ -66,5 +34,4 @@ def process_arguments(args):

if __name__ == "__main__":
args = process_arguments(sys.argv[1:])
print(args)
#pdf_merge(args.inputs, args.output, args.delete)
pdf_merge(args.inputs, args.output, args.delete)
28 changes: 2 additions & 26 deletions pdfrotate.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
#! python3

import os
import sys
import argparse
from tempfile import NamedTemporaryFile
from shutil import move
from glob import glob
from PyPDF2 import PdfFileReader, PdfFileWriter


def pdf_rotate(inputs, counter_clockwise=False):
for input_name in inputs:
filenames = glob(input_name)
for filename in filenames:
with open(filename, 'rb') as f:
writer = PdfFileWriter()
tempfile = NamedTemporaryFile(delete=False)
reader = PdfFileReader(f)
for page in reader.pages:
if counter_clockwise:
writer.addPage(page.rotateCounterClockwise(90))
else:
writer.addPage(page.rotateClockwise(90))
writer.write(tempfile)
f.close()
tempfile.close()
os.remove(filename)
move(tempfile.name, filename)
from pdftools import pdf_rotate


def process_arguments(args):
Expand All @@ -50,4 +26,4 @@ def process_arguments(args):

if __name__ == "__main__":
args = process_arguments(sys.argv[1:])
pdf_rotate(args.inputs, args.counter_clockwise)
pdf_rotate(args.inputs, args.counter_clockwise)
27 changes: 2 additions & 25 deletions pdfsplit.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
#! python3

import os
import sys
import argparse
from PyPDF2 import PdfFileReader, PdfFileWriter
from pdftools import pdf_split


def split_pdf(input, output, stepsize=1):
output = output or os.path.splitext(input)[0]
if not os.path.isfile(input):
print("Error. The file '%s' does not exist." % input)
return
with open(input, "rb") as inputfile:
reader = PdfFileReader(inputfile)
pagenr = 0
outputfile = None
for i, page in enumerate(reader.pages):
if not i % stepsize:
pagenr += 1
outputfile = open(output + "_%i.pdf" % pagenr, "wb")
writer = PdfFileWriter()
writer.addPage(page)
if not (i + 1) % stepsize:
writer.write(outputfile)
outputfile.close()
if not outputfile.closed:
writer.write(outputfile)
outputfile.close()

def process_arguments(args):
parser = argparse.ArgumentParser(description="Split a PDF file in multiple documents.")
#input
Expand All @@ -54,4 +31,4 @@ def process_arguments(args):

if __name__ == "__main__":
args = process_arguments(sys.argv[1:])
split_pdf(args.input, args.output, args.stepsize)
pdf_split(args.input, args.output, args.stepsize)
1 change: 1 addition & 0 deletions pdftools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from pdftools.pdftools import *
Binary file added pdftools/__pycache__/__init__.cpython-34.pyc
Binary file not shown.
Binary file added pdftools/__pycache__/pdftools.cpython-34.pyc
Binary file not shown.
111 changes: 111 additions & 0 deletions pdftools/pdftools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import os
from glob import glob
from tempfile import NamedTemporaryFile
from shutil import move
from PyPDF2 import PdfFileReader, PdfFileWriter


def pdf_merge(inputs, output, delete=False):
writer = PdfFileWriter()
if os.path.isfile(output):
ans = input("The file '%s' already exists. "
"Overwrite? Yes/Abort [Y/a]: " % output).lower()
if ans == "a":
return

outputfile = open(output, "wb")

try:
infiles = []
for filename in inputs:
f = open(filename, 'rb')
reader = PdfFileReader(f)
for page in reader.pages:
writer.addPage(page)
infiles.append(f)
writer.write(outputfile)
except FileNotFoundError as e:
print(e.strerror + ": " + e.filename)
finally:
outputfile.close()
for f in infiles:
f.close()

if delete:
for filename in inputs:
os.remove(filename)


def pdf_rotate(inputs, counter_clockwise=False):
for input_name in inputs:
filenames = glob(input_name)
for filename in filenames:
with open(filename, 'rb') as f:
writer = PdfFileWriter()
tempfile = NamedTemporaryFile(delete=False)
reader = PdfFileReader(f)
for page in reader.pages:
if counter_clockwise:
writer.addPage(page.rotateCounterClockwise(90))
else:
writer.addPage(page.rotateClockwise(90))
writer.write(tempfile)
f.close()
tempfile.close()
os.remove(filename)
move(tempfile.name, filename)


def pdf_split(input, output, stepsize=1):
output = output or os.path.splitext(input)[0]
if not os.path.isfile(input):
print("Error. The file '%s' does not exist." % input)
return
with open(input, "rb") as inputfile:
reader = PdfFileReader(inputfile)
pagenr = 0
outputfile = None
for i, page in enumerate(reader.pages):
if not i % stepsize:
pagenr += 1
outputfile = open(output + "_%i.pdf" % pagenr, "wb")
writer = PdfFileWriter()
writer.addPage(page)
if not (i + 1) % stepsize:
writer.write(outputfile)
outputfile.close()
if not outputfile.closed:
writer.write(outputfile)
outputfile.close()


def pdf_zip(input1, input2, output, delete=False):
writer = PdfFileWriter()
if os.path.isfile(output):
ans = input("The file '%s' already exists. "
"Overwrite? Yes/Abort [Y/a]: " % output).lower()
if ans == "a":
return

outputfile = open(output, "wb")

try:
f1, f2 = open(input1, 'rb'), open(input2, 'rb')
r1, r2 = PdfFileReader(f1), PdfFileReader(f2)
writer = PdfFileWriter()
pages1 = [page for page in r1.pages]
pages2 = [page for page in r2.pages]
for p1, p2 in zip(pages1, pages2):
writer.addPage(p1)
writer.addPage(p2)
writer.write(outputfile)
f1.close()
f2.close()
except FileNotFoundError as e:
print(e.strerror + ": " + e.filename)
finally:
outputfile.close()

if delete:
os.remove(input1)
os.remove(input2)
35 changes: 1 addition & 34 deletions pdfzip.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,8 @@
#! python3

import os
import sys
import argparse
from PyPDF2 import PdfFileReader, PdfFileWriter


def pdf_zip(input1, input2, output, delete=False):
writer = PdfFileWriter()
if os.path.isfile(output):
ans = input("The file '%s' already exists. "
"Overwrite? Yes/Abort [Y/a]: " % output).lower()
if ans == "a":
return

outputfile = open(output, "wb")

try:
f1, f2 = open(input1, 'rb'), open(input2, 'rb')
r1, r2 = PdfFileReader(f1), PdfFileReader(f2)
writer = PdfFileWriter()
pages1 = [page for page in r1.pages]
pages2 = [page for page in r2.pages]
for p1, p2 in zip(pages1, pages2):
writer.addPage(p1)
writer.addPage(p2)
writer.write(outputfile)
f1.close()
f2.close()
except FileNotFoundError as e:
print(e.strerror + ": " + e.filename)
finally:
outputfile.close()

if delete:
for filename in inputs:
os.remove(filename)
from pdftools import pdf_zip


def process_arguments(args):
Expand Down
11 changes: 6 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

setup(
name='pdftools',
version='1.0.1',
packages=[],
version='1.0.2',
packages=['pdftools'],
scripts=['pdfsplit.py', 'pdfmerge.py', 'pdfrotate.py', 'pdfzip.py'],
url='https://github.com/MrLeeh/pdftools',
license='GPL',
license='MIT',
author='Stefan Lehmann',
author_email='[email protected]',
description='small collection of pdf tools',
install_requires=['PyPdf2']
description='small collection of convenience scripts for pdf manipulation',
install_requires=['PyPdf2'],
maintainer='Stefan Lehmann'
)

0 comments on commit 2d8aa28

Please sign in to comment.