Skip to content

Commit

Permalink
Used title for PDF filename
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaid committed Jul 6, 2020
1 parent 58032ef commit 3d020c8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
File renamed without changes.
59 changes: 33 additions & 26 deletions bookdir2pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import argparse, os, sys
from pathlib import Path
import re

# Test if this is a PyInstaller executable or a .py file
if getattr(sys, 'frozen', False):
Expand Down Expand Up @@ -60,6 +61,9 @@ def dir_path(string):

input_dir_name = input_dir.strip(os.path.sep).split(os.path.sep)[-1]

# Get main directory
main_dir = os.path.sep.join(input_dir.rstrip(os.path.sep).split(os.path.sep)[:-1])

# Limit DPI
if args["dpi"] != None:
if (args["dpi"] < 72) or (args["dpi"] > 4800):
Expand Down Expand Up @@ -135,12 +139,40 @@ def dir_path(string):
if args["table_of_contents"] and args["purify"] != None:
print("[WARNING]: Both (--purify|-p) and (--table_of_contents|-c) arguments were passed, will not purify images.")

# Set PDF title
if args["title"] != None:
pdf_title = args["title"]
print()
if len(pdf_title) <= 0:
print("PDF will have no title.")
else:
print("PDF title will be set to '{}'".format(pdf_title))
else:
pdf_title = input_dir_name

# Set PDF author
if args["author"] != None:
pdf_author = args["author"].strip()
print()
if len(pdf_author) <= 0:
print("PDF will have no author.")
else:
print("PDF author will be set to '{}'".format(pdf_author))
else:
pdf_author = PROG_FILE_NAME

def get_valid_filename(s):
s = str(s).strip()
return re.sub(r'(?u)[^-\w.\ ,\!]', '_', s)

if args["table_of_contents"]:
print("Will only print the Table of Contents, will NOT process images or save PDF.")
else:
# Resolve output filename
if args["output_file"] == None:
output_file = input_dir + os.path.extsep + "pdf"
#TODO: Default to title if possible
pdf_title_safe_filename = get_valid_filename(pdf_title)
output_file = os.path.join(main_dir, pdf_title_safe_filename) + os.path.extsep + "pdf"
else:
out_dir, out_name = os.path.split(args["output_file"])
out_name_split = out_name.split(os.path.extsep)
Expand All @@ -163,28 +195,6 @@ def dir_path(string):

output_file_dir, output_file_name = os.path.split(output_file)

# Set PDF title
if args["title"] != None:
pdf_title = args["title"]
print()
if len(pdf_title) <= 0:
print("PDF will have no title.")
else:
print("PDF title will be set to '{}'".format(pdf_title))
else:
pdf_title = input_dir_name

# Set PDF author
if args["author"] != None:
pdf_author = args["author"].strip()
print()
if len(pdf_author) <= 0:
print("PDF will have no author.")
else:
print("PDF author will be set to '{}'".format(pdf_author))
else:
pdf_author = PROG_FILE_NAME

# Do main imports
import img2pdf
from PIL import Image, ImageEnhance
Expand All @@ -210,9 +220,6 @@ def dir_path(string):
valid_exts = ignored_file_exts + page_exts + rename_exts
ignored_file_exts += rename_exts

# Get main directory
main_dir = os.path.sep.join(input_dir.rstrip(os.path.sep).split(os.path.sep)[:-1])

# Prefix to prepend to temporary file/folder names
temp_name_prepend = "__temp__"

Expand Down

0 comments on commit 3d020c8

Please sign in to comment.