Files
epub_bilingual_translator/scripts/pdf_to_epub.py
T

31 lines
2.0 KiB
Python

"""Independent lightweight PDF import. English output can enter the EPUB translator."""
import argparse
import json
import shlex
import sys
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
from src.pdf_import.converter import convert
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('source',type=Path);parser.add_argument('--output',required=True,type=Path)
parser.add_argument('--work',required=True,type=Path);parser.add_argument('--language',choices=['zh-CN','en'],required=True)
parser.add_argument('--title');parser.add_argument('--author');parser.add_argument('--pages',help='1-based inclusive selection, e.g. 1-30,40; output is explicitly partial')
parser.add_argument('--ocr',choices=['auto','native','vision','tesseract'],default='auto')
parser.add_argument('--dpi',type=int,default=180);parser.add_argument('--cover-page',type=int);parser.add_argument('--image-pages',help='Preserve chosen illustration/title/back-cover pages as images, e.g. 2,13,476')
parser.add_argument('--corrections',type=Path,help='Source SHA-bound, exact OCR line corrections JSON')
args=parser.parse_args()
try:
report=convert(args.source,args.output,args.work,args.language,args.title,args.author,args.ocr,args.pages,args.dpi,args.cover_page,image_pages=args.image_pages,corrections=args.corrections)
except (ValueError,RuntimeError,ImportError,OSError) as exc:
parser.exit(1,f'PDF import failed: {exc}\n')
print(json.dumps({k:report[k] for k in ['output','partial','processed_pages','source_pages','engines']},ensure_ascii=False))
if args.language=='en':
translation_python=Path(__file__).resolve().parents[1]/'.venv/bin/python'
print('Optional next step in the translation environment (not executed): '+shlex.join([str(translation_python) if translation_python.exists() else 'python',str(Path(__file__).resolve().parents[1]/'main.py'),str(args.output.resolve()),'--complete-book','--bilingual']))
return 0
if __name__=='__main__':sys.exit(main())