0%

Python批量添加Excel文字到PDF

关键代码:https://stackoverflow.com/questions/47573258/writing-text-over-a-pdf-in-python3
python读取excel数据,添加到pdf对应的页面:
pyinstaller -F pdfWater 打包成 pdfWater.exe,文件名可以拖拽到窗口,效率还可以。60页pdf 3秒左右。

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import xlrd
from datetime import datetime

from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
import os

import reportlab.pdfbase.ttfonts #导入reportlab的注册字体
reportlab.pdfbase.pdfmetrics.registerFont(reportlab.pdfbase.ttfonts.TTFont('song', 'SimSun.ttf')) #注册字体


# 交互
try:
excelName = input("输入excel文件名:")
# excel
# worksheet = xlrd.open_workbook(u'')
worksheet = xlrd.open_workbook(excelName)
sheet_names = worksheet.sheet_names()
print(sheet_names)

sheet = worksheet.sheet_by_name('Sheet1')
rows = sheet.nrows # 获取行数
cols = sheet.ncols # 获取列数
all_content = []
for i in range(rows):
if (i > 0): # 跳过第一行title
cell = sheet.cell_value(i, 2) # 取第二列数据
try:
all_content.append(cell)
except ValueError as e:
pass
print(all_content)
except Exception as ValueError:
print('Excel文件错误:'+ValueError)
# windows
os.system('pause')


try:
pdfName = input("输入pdf文件名:")
# pdf
# read your existing PDF
existing_pdf = PdfFileReader(open(str(pdfName), "rb"))
output = PdfFileWriter()
except Exception as ValueError:
print('Pdf文件错误:'+ValueError)
os.system('pause')


try:
fontSize = int(eval(input("输入字号:")))
if(fontSize < 10):
print("不能小于10:"+fontSize)
except:
print("输入有误,默认为17")
fontSize = 17
finally:
print('Finally fontSize:'+str(fontSize))

try:
outFile = input("生成的pdf文件名(默认2019-01-01.pdf):")
if outFile == "":
outFile = datetime.now().date().strftime('%Y-%m-%d')+".pdf"
print(outFile)
except EOFError:
print("输入有误,默认为日期文件名")
# windows
os.system('pause')


try:
for index in range(len(all_content)):
packet = io.BytesIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
# can.setFontSize(fontSize)
can.setFont('song',fontSize) #设置字体字号
can.setFillColorRGB(1, 0, 0) # choose your font colour
can.drawString(2, 200, all_content[index])
can.save()

# move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)

# 获取PDF文件的页数
pageNum = existing_pdf.getNumPages()
print('ID:' + str(index+1) + ', pdfPageNum:' +
str(index+1) + ', excelLine:' + all_content[index])

# 给每一页打水印
page = existing_pdf.getPage(index)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)

packet.close
packet.flush

# finally, write "output" to a real file
outputStream = open(outFile, "wb")
output.write(outputStream)
outputStream.close()

# windows
os.system('pause')
except Exception as err:
print("程序错误:" + err)

# windows
os.system('pause')