-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfaddimg.py
55 lines (47 loc) · 1.98 KB
/
pdfaddimg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- encoding: utf-8 -*-
"""
@File : test.py
@Time : 2021/1/11 19:12
@Author : SJY
@Email : [email protected]
@Software: PyCharm
@function: 通过索引查找每个同学的照片,插入到pdf中
"""
import os
import tkinter as tk
import img2pdf
import PyPDF2
import xlrd # 读Excel
from tkinter.filedialog import askdirectory,askopenfilename
def pdf2pic(dirpath_, pic_path_,xpath):
source_path = xpath
sourcexls = xlrd.open_workbook(source_path) # 打开学生信息
sourcesheet = sourcexls.sheet_by_index(0) # 第一个工作表
学号 = sourcesheet.col_values(11, 1, None) # 获取列值,用作查找
身份证号 = sourcesheet.col_values(14, 1, None)
for rootd, dirs, files in os.walk(dirpath_):
for item in files:
xh = item.split('.')[0] # 分离学号
if xh in 学号:
sfzh = 身份证号[学号.index(xh)]
file = os.path.join(rootd,item) # 获得所有的pdf文件,附件文件夹
try:
with open("tmp.pdf", "wb") as f: # 临时文件,获取照片到pdf
f.write(img2pdf.convert(f"{pic_path_}/{sfzh}.jpg"))
except:
continue
pdfReader = PyPDF2.PdfFileReader(file) # 读取pdf源文件
pdfWriter = PyPDF2.PdfFileWriter() # 写对象
for i in range(pdfReader.getNumPages()):
pdfWriter.addPage(pdfReader.getPage(i)) # 重写原始pdf文件
pdfReader1 = PyPDF2.PdfFileReader("tmp.pdf") # 读照片pdf文件
pdfWriter.addPage(pdfReader1.getPage(0)) # 添加照片pdf文件
with open(file, "wb") as out: #覆盖原始pdf文件
pdfWriter.write(out)
print(rootd,"正在添加照片")
if __name__ == '__main__':
root = tk.Tk()
root.withdraw()
dirpath = askdirectory(title="pdf文件夹")
pic_path= askdirectory(title=u'毕业照片')
pdf2pic(dirpath,pic_path,"D:/条目","2020")