PDF文档合并-报销单批量粘贴电子发票

用 pymupdf 将电子发票转成图片,再插入报销单中

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
# %%
import os
import fitz
from pprint import pprint

fapiao_dir = 'src/'

zhantiedan = 'zhantiedan_blank.pdf'
ztd_pdf = fitz.open(zhantiedan)

new_name = 'fapiao_dayin.pdf'
new_pdf = fitz.open()


def fapiao_zhantie(fapiao=''):

print(f'\n=== fapiao pdf file:\n {fapiao}')
src_pdf = fitz.open(fapiao)

for p, page in enumerate(src_pdf):
pixmap = page.get_pixmap(dpi=300)
pixmap.save(f'{fapiao}_{str(p)}.png')

new_pdf.insert_pdf(ztd_pdf)
end_page = new_pdf[-1]
end_page.insert_image((100, 100, 700, 500), pixmap=pixmap)

src_pdf.close()

new_pdf.save(new_name)
print(f' {fapiao} :\n*** Done! ***\n')


for dir, folders, files in os.walk(fapiao_dir):
for file in files:
path = os.path.join(dir, file)
print(path)
if path[-4:] == '.pdf':
fapiao_zhantie(path)

ztd_pdf.close()
new_pdf.close()