mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Refactor pdf manipulate commands
This commit is contained in:
parent
7f5a619ad9
commit
697eabe9ae
@ -19,6 +19,7 @@ from calibre.utils.config import OptionParser
|
|||||||
from calibre.utils.logging import Log
|
from calibre.utils.logging import Log
|
||||||
from calibre.constants import preferred_encoding
|
from calibre.constants import preferred_encoding
|
||||||
from calibre.customize.conversion import OptionRecommendation
|
from calibre.customize.conversion import OptionRecommendation
|
||||||
|
from calibre.ebooks.pdf.verify import is_valid_pdf
|
||||||
|
|
||||||
from pyPdf import PdfFileWriter, PdfFileReader
|
from pyPdf import PdfFileWriter, PdfFileReader
|
||||||
|
|
||||||
@ -116,17 +117,6 @@ def crop_pdf(pdf_path, opts, metadata=None):
|
|||||||
|
|
||||||
with open(opts.output, 'wb') as output_file:
|
with open(opts.output, 'wb') as output_file:
|
||||||
output_pdf.write(output_file)
|
output_pdf.write(output_file)
|
||||||
|
|
||||||
# Return True if the pdf is valid.
|
|
||||||
def valid_pdf(pdf_path):
|
|
||||||
try:
|
|
||||||
with open(os.path.abspath(pdf_path), 'rb') as pdf_file:
|
|
||||||
pdf = PdfFileReader(pdf_file)
|
|
||||||
if pdf.isEncrypted or pdf.numPages <= 0:
|
|
||||||
raise Exception
|
|
||||||
except:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def main(args=sys.argv, name=''):
|
def main(args=sys.argv, name=''):
|
||||||
log = Log()
|
log = Log()
|
||||||
@ -141,7 +131,7 @@ def main(args=sys.argv, name=''):
|
|||||||
print_help(parser, log)
|
print_help(parser, log)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if not valid_pdf(args[0]):
|
if not is_valid_pdf(args[0]):
|
||||||
print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % args[0]
|
print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % args[0]
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ from calibre.utils.config import OptionParser
|
|||||||
from calibre.utils.logging import Log
|
from calibre.utils.logging import Log
|
||||||
from calibre.constants import preferred_encoding
|
from calibre.constants import preferred_encoding
|
||||||
from calibre.customize.conversion import OptionRecommendation
|
from calibre.customize.conversion import OptionRecommendation
|
||||||
|
from calibre.ebooks.pdf.verify import is_valid_pdfs
|
||||||
|
|
||||||
from pyPdf import PdfFileWriter, PdfFileReader
|
from pyPdf import PdfFileWriter, PdfFileReader
|
||||||
|
|
||||||
@ -56,17 +57,6 @@ def print_info(pdf_path):
|
|||||||
print _('PDF Version: %s' % mo.group('version'))
|
print _('PDF Version: %s' % mo.group('version'))
|
||||||
except: pass
|
except: pass
|
||||||
|
|
||||||
def verify_files(files):
|
|
||||||
invalid = []
|
|
||||||
|
|
||||||
for pdf_path in files:
|
|
||||||
try:
|
|
||||||
with open(os.path.abspath(pdf_path), 'rb') as pdf_file:
|
|
||||||
pdf = PdfFileReader(pdf_file)
|
|
||||||
except:
|
|
||||||
invalid.append(pdf_path)
|
|
||||||
return invalid
|
|
||||||
|
|
||||||
def main(args=sys.argv, name=''):
|
def main(args=sys.argv, name=''):
|
||||||
log = Log()
|
log = Log()
|
||||||
parser = option_parser(name)
|
parser = option_parser(name)
|
||||||
@ -79,7 +69,7 @@ def main(args=sys.argv, name=''):
|
|||||||
print_help(parser, log)
|
print_help(parser, log)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
bad_pdfs = verify_files(args)
|
bad_pdfs = is_valid_pdfs(args)
|
||||||
if bad_pdfs != []:
|
if bad_pdfs != []:
|
||||||
for pdf in bad_pdfs:
|
for pdf in bad_pdfs:
|
||||||
print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % pdf
|
print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % pdf
|
||||||
|
@ -18,6 +18,7 @@ from calibre.utils.config import OptionParser
|
|||||||
from calibre.utils.logging import Log
|
from calibre.utils.logging import Log
|
||||||
from calibre.constants import preferred_encoding
|
from calibre.constants import preferred_encoding
|
||||||
from calibre.customize.conversion import OptionRecommendation
|
from calibre.customize.conversion import OptionRecommendation
|
||||||
|
from calibre.ebooks.pdf.verify import is_valid_pdfs
|
||||||
|
|
||||||
from pyPdf import PdfFileWriter, PdfFileReader
|
from pyPdf import PdfFileWriter, PdfFileReader
|
||||||
|
|
||||||
@ -76,19 +77,6 @@ def merge_files(in_paths, out_path, metadata=None):
|
|||||||
|
|
||||||
with open(out_path, 'wb') as out_file:
|
with open(out_path, 'wb') as out_file:
|
||||||
out_pdf.write(out_file)
|
out_pdf.write(out_file)
|
||||||
|
|
||||||
def verify_files(files):
|
|
||||||
invalid = []
|
|
||||||
|
|
||||||
for pdf_path in files:
|
|
||||||
try:
|
|
||||||
with open(os.path.abspath(pdf_path), 'rb') as pdf_file:
|
|
||||||
pdf = PdfFileReader(pdf_file)
|
|
||||||
if pdf.isEncrypted or pdf.numPages <= 0:
|
|
||||||
raise Exception
|
|
||||||
except:
|
|
||||||
invalid.append(pdf_path)
|
|
||||||
return invalid
|
|
||||||
|
|
||||||
def main(args=sys.argv, name=''):
|
def main(args=sys.argv, name=''):
|
||||||
log = Log()
|
log = Log()
|
||||||
@ -103,7 +91,7 @@ def main(args=sys.argv, name=''):
|
|||||||
print_help(parser, log)
|
print_help(parser, log)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
bad_pdfs = verify_files(args)
|
bad_pdfs = is_valid_pdfs(args)
|
||||||
if bad_pdfs != []:
|
if bad_pdfs != []:
|
||||||
for pdf in bad_pdfs:
|
for pdf in bad_pdfs:
|
||||||
print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % pdf
|
print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % pdf
|
||||||
|
@ -18,6 +18,7 @@ from calibre.utils.config import OptionParser
|
|||||||
from calibre.utils.logging import Log
|
from calibre.utils.logging import Log
|
||||||
from calibre.constants import preferred_encoding
|
from calibre.constants import preferred_encoding
|
||||||
from calibre.customize.conversion import OptionRecommendation
|
from calibre.customize.conversion import OptionRecommendation
|
||||||
|
from calibre.ebooks.pdf.verify import is_valid_pdf
|
||||||
|
|
||||||
from pyPdf import PdfFileWriter, PdfFileReader
|
from pyPdf import PdfFileWriter, PdfFileReader
|
||||||
|
|
||||||
@ -74,17 +75,6 @@ def reverse(pdf_path, out_path, metadata=None):
|
|||||||
with open(out_path, 'wb') as out_file:
|
with open(out_path, 'wb') as out_file:
|
||||||
out_pdf.write(out_file)
|
out_pdf.write(out_file)
|
||||||
|
|
||||||
# Return True if the pdf is valid.
|
|
||||||
def valid_pdf(pdf_path):
|
|
||||||
try:
|
|
||||||
with open(os.path.abspath(pdf_path), 'rb') as pdf_file:
|
|
||||||
pdf = PdfFileReader(pdf_file)
|
|
||||||
if pdf.isEncrypted or pdf.numPages <= 0:
|
|
||||||
raise Exception
|
|
||||||
except:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def main(args=sys.argv, name=''):
|
def main(args=sys.argv, name=''):
|
||||||
log = Log()
|
log = Log()
|
||||||
parser = option_parser(name)
|
parser = option_parser(name)
|
||||||
@ -98,7 +88,7 @@ def main(args=sys.argv, name=''):
|
|||||||
print_help(parser, log)
|
print_help(parser, log)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if not valid_pdf(args[0]):
|
if not is_valid_pdf(args[0]):
|
||||||
print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % args[0]
|
print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % args[0]
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ from calibre.utils.config import OptionParser
|
|||||||
from calibre.utils.logging import Log
|
from calibre.utils.logging import Log
|
||||||
from calibre.constants import preferred_encoding
|
from calibre.constants import preferred_encoding
|
||||||
from calibre.customize.conversion import OptionRecommendation
|
from calibre.customize.conversion import OptionRecommendation
|
||||||
|
from calibre.ebooks.pdf.verify import is_valid_pdf
|
||||||
|
|
||||||
from pyPdf import PdfFileWriter, PdfFileReader
|
from pyPdf import PdfFileWriter, PdfFileReader
|
||||||
|
|
||||||
@ -163,17 +164,6 @@ def clean_page_list(pdf_path, pages, page_ranges):
|
|||||||
|
|
||||||
return pages, page_ranges
|
return pages, page_ranges
|
||||||
|
|
||||||
# Return True if the pdf is valid.
|
|
||||||
def valid_pdf(pdf_path):
|
|
||||||
try:
|
|
||||||
with open(os.path.abspath(pdf_path), 'rb') as pdf_file:
|
|
||||||
pdf = PdfFileReader(pdf_file)
|
|
||||||
if pdf.isEncrypted or pdf.numPages <= 0:
|
|
||||||
raise Exception
|
|
||||||
except:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def main(args=sys.argv, name=''):
|
def main(args=sys.argv, name=''):
|
||||||
log = Log()
|
log = Log()
|
||||||
parser = option_parser(name)
|
parser = option_parser(name)
|
||||||
@ -194,7 +184,7 @@ def main(args=sys.argv, name=''):
|
|||||||
print_help(parser, log)
|
print_help(parser, log)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
if not valid_pdf(pdf):
|
if not is_valid_pdf(pdf):
|
||||||
print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % pdf
|
print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % pdf
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
37
src/calibre/ebooks/pdf/verify.py
Normal file
37
src/calibre/ebooks/pdf/verify.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
from __future__ import with_statement
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
__license__ = 'GPL v3'
|
||||||
|
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
|
||||||
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
|
'''
|
||||||
|
Verify PDF files.
|
||||||
|
'''
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from pyPdf import PdfFileWriter, PdfFileReader
|
||||||
|
|
||||||
|
def is_valid_pdf(pdf_path):
|
||||||
|
'''
|
||||||
|
Returns True if the pdf file is valid.
|
||||||
|
'''
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(os.path.abspath(pdf_path), 'rb') as pdf_file:
|
||||||
|
pdf = PdfFileReader(pdf_file)
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def is_valid_pdfs(pdf_paths):
|
||||||
|
'''
|
||||||
|
Returns a list of invalid pdf files.
|
||||||
|
'''
|
||||||
|
|
||||||
|
invalid = []
|
||||||
|
for pdf_path in pdf_paths:
|
||||||
|
if not is_valid_pdf(pdf_path):
|
||||||
|
invalid.append(pdf_path)
|
||||||
|
return invalid
|
Loading…
x
Reference in New Issue
Block a user