我正在尝试制作一个GUI,以便用户可以编辑/修改自己的用户输入'x1'或'x2'等,如代码中所示。我的理解是,这些是函数中函数的变量。因此,我发现在逻辑上跳到下一步非常具有挑战性。您的建议将不胜感激。提前致谢, 乔治
#Form filling mapping
import io
import pdfrw
from reportlab.pdfgen import canvas
def run():
canvas_data = get_overlay_canvas()
form = merge(canvas_data, template_path='./SmartInvoice.pdf')
save(form, filename='merged.pdf')
def get_overlay_canvas() -> io.BytesIO:
data = io.BytesIO()
pdf = canvas.Canvas(data)
#Invoice issuer, company name etc:
pdf.drawString(x=100, y=727, text='x1')
#Issuer address
pdf.drawString(x=100, y=711, text='x2')
#ABN or ACN
pdf.drawString(x=100, y=696, text='x3')
#Phone Contact
pdf.drawString(x=100, y=681, text='x4')
#Invoice issued to:
pdf.drawString(x=83, y=606, text='x5')
#Customer Details:
pdf.drawString(x=83, y=591, text='x6')
#Tax Invoice No.
pdf.drawString(x=535, y=674, text='x7')
#Date of issuance of invoice
pdf.drawString(x=516, y=658, text='x8')
#Form Description
pdf.drawString(x=33, y=650, text='x9')
#Subtotal
pdf.drawString(x=500, y=458, text='$1,000,000')
#Subtotal 2
pdf.drawString(x=500, y=400, text='$1,500,000')
#Subtotal 3
pdf.drawString(x=500, y=342, text='$4,000,000')
#Total
pdf.drawString(x=500, y=237, text='$6,500,000')
#GST
pdf.drawString(x=500, y=210, text='$715,000')
#Total Inc. GST
pdf.drawString(x=500, y=178, text='$7,150,000')
#Description of good or service provided.
pdf.drawString(x=45, y=465, text='x23')
#Payment methods:
pdf.drawString(x=150, y=120, text='x22')
#Payment due by date:
pdf.drawString(x=86, y=151, text='16/08/2018')
pdf.save()
data.seek(0)
return data
def merge(overlay_canvas: io.BytesIO, template_path: str) -> io.BytesIO:
template_pdf = pdfrw.PdfReader(template_path)
overlay_pdf = pdfrw.PdfReader(overlay_canvas)
for page, data in zip(template_pdf.pages, overlay_pdf.pages):
overlay = pdfrw.PageMerge().add(data)[0]
pdfrw.PageMerge(page).add(overlay).render()
form = io.BytesIO()
pdfrw.PdfWriter().write(form, template_pdf)
form.seek(0)
return form
def save(form: io.BytesIO, filename: str):
with open(filename, 'wb') as f:
f.write(form.read())
if __name__ == '__main__':
run()