首页 > 解决方案 > jpg到pdf的批量转换

问题描述

我有大量的 jpeg 文件,每个文件都是历史文档中一页的照片。现在我想(批量)从这些文件中创建 pdf 文件,最好将那些代表一个文档的文件制作成单独的 pdf 文件,并且页面的顺序正确。文件名的构造类似于“date yp id optional.jpg”,如果多个文档具有相同的日期,则 y 是运行编号,p 是页码,id 是来自相机的照片编号,最后可选有时存在,并且包含有关文档的可选信息。所有的部分都用一个空格隔开。我希望找到一种使用内置 Microsoft PDF 编写器的可能性,但还没有找到一个命令行界面。我当然可以从目录列表中制作一个脚本,只有我知道要为其制作脚本的应用程序的命令行界面。如果创建的 pdf 文件的每一页都可以包含文件名的一部分,那将是一个好处。

标签: pdfcommand-linejpeg

解决方案


如果您不反对 python 脚本,则有一个名为 img2pdf 的 pdf 库的图像。PyPi 链接可以在这里找到,我很乐意为您编写一个快速脚本

编辑:可以在这里找到教程

编辑2:这应该做

## Import libraries ##
import img2pdf, os
from Pillow import Image
# sets an empty list var to store the dir
dirofjpgs = "PUT DIRECTORY HERE" # formatting is C:\\User not C:\User\
pathforpdfs = "PUT DIRECTORY HERE"
# change dir to working dir
os.chdir(dirofjpgs)
NameOfFiles = []
# sets and empty list to store the names of files
ExtOfFiles = []
# sets and empty list to store the names and extentions of files
self_file = os.path.basename(__file__)
for i in range(1, len(os.listdir(os.curdir))): # for every item in the current dir
    if (os.path.splitext((os.listdir(os.curdir))[i])[1]) != ".ini": # if the item ends doesnt end in .ini which is a windows file
        NameOfFiles.append(os.path.splitext((os.listdir(os.curdir))[i])[0])  # adds the Name of the file into the NameOfFiles list
        ExtOfFiles.append(os.path.splitext((os.listdir(os.curdir))[i])[1]) # adds the Name and Extention of the file into the ExtOfFiles list

# for every item in the nameoffiles list
for i in range(len(NameOfFiles)):
    # open image with pillow
    image = Image.open(NameOfFiles[i], ExtOfFiles[i])
    # convert with img2pdf
    pdf_values = img2pdf.convert(image.filename)
    # save as pdf in dir
    file = open(pathforpdfs, "wb")
    file.write(pdf_values)
    #close
    image.close()
    file.close()
    print(str(i+1), "/", len(NameOfFiles))

mg2pdf-模块/


推荐阅读