X-Git-Url: https://plomlompom.com/repos/bar%20baz.html?a=blobdiff_plain;ds=sidebyside;f=bookmaker.py;h=b9daa0b332d6b532b277bd39c78a30bed4157615;hb=e0da0a9197adced52d4fd936fa6d309040de8feb;hp=2211b848460d83aabc41f9bd89209e4c2b63be49;hpb=62a5f685da1cbfc256b712d6c0b1222cf19ec1ea;p=misc diff --git a/bookmaker.py b/bookmaker.py index 2211b84..b9daa0b 100755 --- a/bookmaker.py +++ b/bookmaker.py @@ -36,6 +36,7 @@ def parse_page_range(range_string, pages): return start_page, end_page pages_to_add = [] opened_files = [] +new_page_num = 0 for i, input_file in enumerate(args.input_file): file = open(input_file, 'rb') opened_files += [file] @@ -44,10 +45,11 @@ for i, input_file in enumerate(args.input_file): if args.page_range and len(args.page_range) > i: range_string = args.page_range[i] start_page, end_page = parse_page_range(range_string, reader.pages) - for page_num in range(start_page, end_page): - page = reader.pages[page_num] + for old_page_num in range(start_page, end_page): + new_page_num += 1 + page = reader.pages[old_page_num] pages_to_add += [page] - print("read in %s page number %d" % (input_file, page_num+1)) + print("-i, -p: read in %s page number %d as new page %d" % (input_file, old_page_num+1, new_page_num)) # rotate page canvas if args.rotate: @@ -56,6 +58,7 @@ if args.rotate: page.add_transformation(pypdf.Transformation().translate(tx=-a4_width/2, ty=-a4_height/2)) page.add_transformation(pypdf.Transformation().rotate(-90)) page.add_transformation(pypdf.Transformation().translate(tx=a4_width/2, ty=a4_height/2)) + print("-r: rotating (by 90°) page", rotate) # normalize all pages to portrait A4 for page in pages_to_add: @@ -81,6 +84,10 @@ if args.crop_range: crops = initial_split[0] start_page, end_page = parse_page_range(page_range, pages_to_add) crop_left, crop_bottom, crop_right, crop_top = [float(x) * points_per_cm for x in crops.split(',')] + if args.symmetry: + print("-c, -t: to pages %d to %d applying crops: left %dcm, bottom %dcm, right %dcm, top %dcm (but alternating left and right crop between even and odd pages)" % (start_page + 1, end_page, crop_left, crop_bottom, crop_right, crop_top)) + else: + print("-c: to pages %d to %d applying crops: left %dcm, bottom %dcm, right %dcm, top %dcm" % (start_page + 1, end_page, crop_left, crop_bottom, crop_right, crop_top)) cropped_width = a4_width - crop_left - crop_right cropped_height = a4_height - crop_bottom - crop_top zoom = 1 @@ -94,7 +101,10 @@ if args.crop_range: else: zoom = max(zoom_horizontal, zoom_vertical) for page_num in range(start_page, end_page): - crops_at_page[page_num] = (crop_left, crop_bottom, crop_right, crop_top) + if args.symmetry and page_num % 2: + crops_at_page[page_num] = (crop_right, crop_bottom, crop_left, crop_top) + else: + crops_at_page[page_num] = (crop_left, crop_bottom, crop_right, crop_top) zoom_at_page[page_num] = zoom writer = pypdf.PdfWriter() @@ -103,10 +113,7 @@ if not args.nup4: for i, page in enumerate(pages_to_add): crop_left, crop_bottom, crop_right, crop_top = crops_at_page[i] zoom = zoom_at_page[i] - if args.symmetry and odd_page: - page.add_transformation(pypdf.Transformation().translate(tx=-crop_right, ty=-crop_bottom)) - else: - page.add_transformation(pypdf.Transformation().translate(tx=-crop_left, ty=-crop_bottom)) + page.add_transformation(pypdf.Transformation().translate(tx=-crop_left, ty=-crop_bottom)) page.add_transformation(pypdf.Transformation().scale(zoom, zoom)) cropped_width = a4_width - crop_left - crop_right cropped_height = a4_height - crop_bottom - crop_top @@ -172,15 +179,9 @@ else: zoom = zoom_at_page[new_i] page.add_transformation(pypdf.Transformation().translate(ty=(a4_height / zoom - (a4_height - crop_top)))) if i == 0 or i == 2: - if args.symmetry: - page.add_transformation(pypdf.Transformation().translate(tx=-crop_left)) - else: - page.add_transformation(pypdf.Transformation().translate(tx=-crop_right)) + page.add_transformation(pypdf.Transformation().translate(tx=-crop_left)) elif i == 1 or i == 3: - if args.symmetry: - page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_left)))) - else: - page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_right)))) + page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_right)))) page.add_transformation(pypdf.Transformation().scale(zoom * bonus_shrink_factor, zoom * bonus_shrink_factor)) if i == 2 or i == 3: page.add_transformation(pypdf.Transformation().translate(ty=-2*printable_margin/printable_scale)) @@ -266,8 +267,9 @@ else: writer.add_page(new_page) i = 0 front_page = not front_page + +# write and close for file in opened_files: file.close() - with open(args.output_file, 'wb') as output_file: writer.write(output_file)