mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
bzr 'command sub-command' style wrapper for pdf manipulation
This commit is contained in:
parent
a5228d56d2
commit
ffa5f36fae
67
src/calibre/ebooks/pdf/manipulate.py
Normal file
67
src/calibre/ebooks/pdf/manipulate.py
Normal file
@ -0,0 +1,67 @@
|
||||
'''
|
||||
Command line interface to run pdf manipulation commands.
|
||||
'''
|
||||
from __future__ import with_statement
|
||||
|
||||
__license__ = 'GPL v3'
|
||||
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import string, sys
|
||||
|
||||
from calibre.utils.config import Config, StringConfig
|
||||
from calibre.ebooks.pdf import merge, split, trim
|
||||
|
||||
COMMANDS = {
|
||||
'merge' : merge,
|
||||
'split' : split,
|
||||
'trim' : trim,
|
||||
}
|
||||
|
||||
def config(defaults=None):
|
||||
desc = _('Options to control the transformation of pdf')
|
||||
if defaults is None:
|
||||
c = Config('trimpdf', desc)
|
||||
else:
|
||||
c = StringConfig(defaults, desc)
|
||||
return c
|
||||
|
||||
def option_parser():
|
||||
c = config()
|
||||
return c.option_parser(usage=_('''\
|
||||
|
||||
%prog command ...
|
||||
|
||||
command can be one of the following:
|
||||
[%%commands]
|
||||
|
||||
Use %prog command --help to get more information about a specific command
|
||||
|
||||
Manipulate a PDF.
|
||||
'''.replace('%%commands', string.join(sorted(COMMANDS.keys()), ', '))))
|
||||
|
||||
def main(args=sys.argv):
|
||||
parser = option_parser()
|
||||
|
||||
if len(args) < 2:
|
||||
print 'Error: No command sepecified.\n'
|
||||
print parser.get_usage()
|
||||
return 2
|
||||
|
||||
command = args[1].lower().strip()
|
||||
|
||||
if command in COMMANDS.keys():
|
||||
del args[1]
|
||||
return COMMANDS[command].main(args, command)
|
||||
else:
|
||||
parser.parse_args(args)
|
||||
print 'Unknown command %s.\n' % command
|
||||
print parser.get_usage()
|
||||
return 2
|
||||
|
||||
# We should never get here.
|
||||
return 0
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
@ -17,9 +17,8 @@ from pyPdf import PdfFileWriter, PdfFileReader
|
||||
|
||||
def config(defaults=None):
|
||||
desc = _('Options to control the transformation of pdf')
|
||||
default_crop=10
|
||||
if defaults is None:
|
||||
c = Config('trimpdf', desc)
|
||||
c = Config('mergepdf', desc)
|
||||
else:
|
||||
c = StringConfig(defaults, desc)
|
||||
c.add_opt('verbose', ['-v', '--verbose'], default=0, action='count',
|
||||
@ -28,13 +27,13 @@ def config(defaults=None):
|
||||
help=_('Path to output file. By default a file is created in the current directory.'))
|
||||
return c
|
||||
|
||||
def option_parser():
|
||||
def option_parser(name):
|
||||
c = config()
|
||||
return c.option_parser(usage=_('''\
|
||||
%prog [options] file1.pdf file2.pdf ...
|
||||
%prog %%name [options] file1.pdf file2.pdf ...
|
||||
|
||||
Merges individual PDFs. Metadata will be used from the first PDF specified.
|
||||
'''))
|
||||
'''.replace('%%name', name)))
|
||||
|
||||
def merge_files(in_paths, out_path, metadata=None):
|
||||
if metadata == None:
|
||||
@ -67,8 +66,8 @@ def verify_files(files):
|
||||
invalid.append(pdf_path)
|
||||
return invalid
|
||||
|
||||
def main(args=sys.argv):
|
||||
parser = option_parser()
|
||||
def main(args=sys.argv, name=''):
|
||||
parser = option_parser(name)
|
||||
opts, args = parser.parse_args(args)
|
||||
args = args[1:]
|
||||
|
@ -17,9 +17,8 @@ from pyPdf import PdfFileWriter, PdfFileReader
|
||||
|
||||
def config(defaults=None):
|
||||
desc = _('Options to control the transformation of pdf')
|
||||
default_crop=10
|
||||
if defaults is None:
|
||||
c = Config('trimpdf', desc)
|
||||
c = Config('splitpdf', desc)
|
||||
else:
|
||||
c = StringConfig(defaults, desc)
|
||||
c.add_opt('verbose', ['-v', '--verbose'], default=0, action='count',
|
||||
@ -29,21 +28,21 @@ def config(defaults=None):
|
||||
The file name will be the base name for the output.'))
|
||||
return c
|
||||
|
||||
def option_parser():
|
||||
def option_parser(name):
|
||||
c = config()
|
||||
return c.option_parser(usage=_('''\
|
||||
|
||||
%prog [options] file.pdf page_to_split_on ...
|
||||
%prog [options] file.pdf page_range_to_split_on ...
|
||||
%prog %%name [options] file.pdf page_to_split_on ...
|
||||
%prog %%name [options] file.pdf page_range_to_split_on ...
|
||||
|
||||
Ex.
|
||||
|
||||
%prog file.pdf 6
|
||||
%prog file.pdf 6-12
|
||||
%prog file.pdf 6-12 8 10 9-20
|
||||
%prog %%name file.pdf 6
|
||||
%prog %%name file.pdf 6-12
|
||||
%prog %%name file.pdf 6-12 8 10 9-20
|
||||
|
||||
Split a PDF.
|
||||
'''))
|
||||
'''.replace('%%name', name)))
|
||||
|
||||
def split_pdf(in_path, pages, page_ranges, out_name, metadata=None):
|
||||
pdf = PdfFileReader(open(os.path.abspath(in_path), 'rb'))
|
||||
@ -155,8 +154,8 @@ def valid_pdf(pdf_path):
|
||||
return False
|
||||
return True
|
||||
|
||||
def main(args=sys.argv):
|
||||
parser = option_parser()
|
||||
def main(args=sys.argv, name=''):
|
||||
parser = option_parser(name)
|
||||
opts, args = parser.parse_args(args)
|
||||
|
||||
pdf, pages, page_ranges, unknown = split_args(args[1:])
|
@ -33,16 +33,16 @@ def config(defaults=None):
|
||||
return c
|
||||
|
||||
|
||||
def option_parser():
|
||||
def option_parser(name):
|
||||
c = config()
|
||||
return c.option_parser(usage=_('''\
|
||||
%prog [options] file.pdf
|
||||
%prog %%name [options] file.pdf
|
||||
|
||||
Crops a pdf.
|
||||
'''))
|
||||
'''.replace('%%name', name)))
|
||||
|
||||
def main(args=sys.argv):
|
||||
parser = option_parser()
|
||||
def main(args=sys.argv, name=''):
|
||||
parser = option_parser(name)
|
||||
opts, args = parser.parse_args(args)
|
||||
try:
|
||||
source = os.path.abspath(args[1])
|
@ -39,10 +39,7 @@ entry_points = {
|
||||
'calibre-fontconfig = calibre.utils.fontconfig:main',
|
||||
'calibre-parallel = calibre.parallel:main',
|
||||
'calibre-customize = calibre.customize.ui:main',
|
||||
'pdftrim = calibre.ebooks.pdf.pdftrim:main',
|
||||
'pdfmerge = calibre.ebooks.pdf.pdfmerge:main',
|
||||
'pdfsplit = calibre.ebooks.pdf.pdfsplit:main',
|
||||
'fetch-ebook-metadata = calibre.ebooks.metadata.fetch:main',
|
||||
'pdfmanipulate = calibre.ebooks.pdf.manipulate:main',
|
||||
],
|
||||
'gui_scripts' : [
|
||||
__appname__+' = calibre.gui2.main:main',
|
||||
|
Loading…
x
Reference in New Issue
Block a user