经常扫描的时候通过自动扫描仪,正面扫一次,反面扫一次,但是反面是逆页序的,如果手工拼在一起,特别费劲。所以可以用python写个小工具自动拼在一起,扫描完成后,奇数页命名为odd.pdf,偶数页命名为even.pdf,和这个小工具放在一个目录下,就可以实现自动合并。PyPDF2这个package不能用3.0版本及往上的,后面的函数名改了,2.12.1版本没问题。

import os
import ntpath
import sys

import PyPDF2

class File:
    def __init__(self, path: str):
        if not ntpath.exists(path):
            print('文件不存在')
            raise FileNotFoundError
        self.abspath = ntpath.abspath(path)
        self.filetype = ntpath.splitext(self.abspath)[1]
        self.filename = ntpath.splitext(ntpath.basename(self.abspath))[0]
        self.dirpath = ntpath.dirname(self.abspath) + '\\'

def merge(file1, file2):
    a = File(file1)
    b = File(file2)
    if a.filetype != '.pdf' or b.filetype != '.pdf':
        print('文件类型错误')
        os.system('pause')
        exit(1)
    f1 = open('odd.pdf', 'rb')
    f2 = open('even.pdf', 'rb')
    reader1 = PyPDF2.PdfFileReader(f1)
    reader2 = PyPDF2.PdfFileReader(f2)
    writer = PyPDF2.PdfFileWriter()
    if reader1.numPages != reader2.numPages:
        print('页数不相等')
        os.system('pause')
        exit(2)
    for i in range(reader1.numPages):
        writer.addPage(reader1.getPage(i))
        writer.addPage(reader2.getPage(-(i + 1)))
    newf = open('combine.pdf', "wb")
    writer.write(newf)
    f1.close()
    f2.close()
    newf.close()


def main():
    import sys, os

    config_name = 'myapp.cfg'

    if getattr(sys, 'frozen', False):
        application_path = os.path.dirname(sys.executable)
        running_mode = 'Frozen/executable'
    else:
        try:
            app_full_path = os.path.realpath(__file__)
            application_path = os.path.dirname(app_full_path)
            running_mode = "Non-interactive (e.g. 'python myapp.py')"
        except NameError:
            application_path = os.getcwd()
            running_mode = 'Interactive'

    config_full_path = os.path.join(application_path, config_name)

    print('Running mode:', running_mode)
    print('  Appliction path  :', application_path)
    os.chdir(application_path)

    merge("odd.pdf","even.pdf")
  #  args: list = sys.argv[1:]
 #   i = len(args)
  #  if i >= 2 and i % 2 == 0:
  #      args.sort()
  #      for j in range(0, i, 2):
  #          merge(args[j], args[j + 1])


if __name__ == '__main__':
    main()

我这里编译好的可以直接运行的版本