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秒左右。

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')

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注