-
Notifications
You must be signed in to change notification settings - Fork 22
/
djvu2pdf.py
executable file
·102 lines (85 loc) · 3.79 KB
/
djvu2pdf.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python2
# -*- coding: utf8 -*-
"""
djvu2pdf.py: Converts a .djvu file into a .pdf file
"""
#==============================================================================
# This Script does exactly as the description above says.
#==============================================================================
#==============================================================================
# Copyright 2011 joe di castro <joe@joedicastro.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#==============================================================================
__author__ = "joe di castro <joe@joedicastro.com>"
__license__ = "GNU General Public License version 3"
__date__ = "03/12/2011"
__version__ = "0.3"
try:
import sys
import os
from argparse import ArgumentParser
from subprocess import Popen, PIPE
except ImportError:
# Checks the installation of the necessary python modules
print((os.linesep * 2).join(["An error found importing one module:",
str(sys.exc_info()[1]), "You need to install it", "Stopping..."]))
sys.exit(-2)
def check_execs(*progs):
"""Check if the programs are installed, if not exit and report."""
for prog in progs:
try:
Popen([prog, '--help'], stdout=PIPE, stderr=PIPE)
except OSError:
msg = 'The {0} program is necessary to run the script'.format(prog)
sys.exit(msg)
return
def arguments():
"""Defines the command line arguments for the script."""
main_desc = """Converts a djvu file into a pdf file"""
parser = ArgumentParser(description=main_desc)
parser.add_argument("file", nargs="+", help="The djvu file")
group = parser.add_mutually_exclusive_group()
group.add_argument("-d", dest="qlty", action="store_const", const="-d",
help="no compression. Best quality but big files.")
group.add_argument("-z", dest="qlty", action="store_const", const="-z",
help="zip compression. More quality, more size.")
parser.add_argument("-v", "--version", action="version",
version="%(prog)s {0}".format(__version__),
help="show program's version number and exit")
return parser
def process(command, fname):
"""Process the external commands and report the errors."""
errors = Popen(command, stderr=PIPE).stderr.readlines()
for line in errors:
print("{0}: {1}".format(fname.upper(), line.rstrip(os.linesep)))
def main():
"""Main section."""
args = arguments().parse_args()
djvu_files = args.file
for djvu in djvu_files:
if not os.path.exists(djvu):
print("ERROR: cannot open '{0}' (No such file)".format(djvu))
else:
djvu_filename = djvu.split(".djvu")[0]
tiff = '{0}.tif'.format(djvu_filename)
pdf = '{0}.pdf'.format(djvu_filename)
process(['ddjvu', '-format=tiff', djvu, tiff], tiff)
if os.path.exists(tiff):
quality = args.qlty if args.qlty else "-j"
process(['tiff2pdf', quality, '-o', pdf, tiff], pdf)
os.remove(tiff)
if __name__ == "__main__":
check_execs('ddjvu', 'tiff2pdf')
main()