pdf - jpg到pdf的批量转换
问题描述
我有大量的 jpeg 文件,每个文件都是历史文档中一页的照片。现在我想(批量)从这些文件中创建 pdf 文件,最好将那些代表一个文档的文件制作成单独的 pdf 文件,并且页面的顺序正确。文件名的构造类似于“date yp id optional.jpg”,如果多个文档具有相同的日期,则 y 是运行编号,p 是页码,id 是来自相机的照片编号,最后可选有时存在,并且包含有关文档的可选信息。所有的部分都用一个空格隔开。我希望找到一种使用内置 Microsoft PDF 编写器的可能性,但还没有找到一个命令行界面。我当然可以从目录列表中制作一个脚本,只有我知道要为其制作脚本的应用程序的命令行界面。如果创建的 pdf 文件的每一页都可以包含文件名的一部分,那将是一个好处。
解决方案
如果您不反对 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-模块/
推荐阅读
- javascript - Ember 应用程序控制器共享数据
- java - 如何在 Java/Android 中以最有效的方式读取 txt 复杂的 hashmap
- python - 使用嵌入式 Python 调用对象实例的函数
- ios - 由于“内部”保护级别,无法访问 phoneFieldChanged
- android - Android In-App-Billing - Live App 某些用户的错误代码 5
- cakephp-3.0 - Cakephp 3 如何从实体生成表模式?
- react-native - 安装 react native - 命令行界面(环境:节点:没有这样的文件或目录)
- django - 将域添加到在数字海洋上运行的 django webapp
- html - 滚动时嵌入的 iframe 视频与固定导航栏重叠
- php - 在 laravel 分页中制作干净的 url