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

  • pymupdf
    • pip install pymupdf
    • https://pymupdf.readthedocs.io/en/latest/index.html

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

# %%
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()


你可能感兴趣的:(PDF文档合并-报销单批量粘贴电子发票)