Reportlabs 写入多个页面作为运输标签(整页)Python

Reportlabs writing to multiple pages as shipping labels (full page) Python

我对使用 Reportlabs 还很陌生,我正在尝试找到一种更好的方法来生成如下所示的整页运输标签:

到目前为止我有这段代码(虽然写得不好):

import datetime

from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

year = datetime.date.today().year

# IIRNumber = str(int(input("Month (digit): "))) + "-" + str(int(input("Shipment Number (include zero if before 10): ")))
# VMBENumber = "VBME-" + str(year)[-2:] + "-" + str(int(input("Enter VBME Number (last four digits: ")))
# PONUmber = input("Enter PO number or V for Various: ")
IIRNumber = "08-09"
VMBENumber = "VBME-21-1458"
PONUmber = "9185555"

ShipmentNumber = int(input("How many pieces in the shipment?: "))
print(VMBENumber)
print(IIRNumber)
print(ShipmentNumber)


def textobject_demo():

    my_canvas = canvas.Canvas("PDFGenerated.pdf",
                              pagesize=letter)
    # Create text_object
    text_object = my_canvas.beginText()
    # Set text location (x, y)


    # Write a line of text + carriage return
    # Set font face and size
    text_object.setFont('Times-Roman', 12)
    text_object.setTextOrigin(150, 730)

    for i in range(1, ShipmentNumber + 1):

        # Write text to the canvas
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text='To: Voisey Bay Mine Site – EXPANSION PROJECT (VBME)')
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="             C/O Innu Mikun Hangar")
        text_object.textLine(text="             Happy Valley-Goose Bay, NL")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="                     CC0012- " + VMBENumber + " - " + IIRNumber)
        text_object.textLine(text="                     Product Description:" + PONUmber)
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="                         Box " + str(i) + " of " + str(ShipmentNumber))
        text_object.textLine(text="                         Gross Weight: 14 lbs")
        text_object.textLine(text="                         Net Weight: 14 lbs")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="                         Dimensions: 9 L x 9 W x 9” H")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="             Special Storage Instruction: NO")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="")
        text_object.textLine(text="             Attn:   IIR – REDPATH Canada Limited")
        text_object.textLine(text="                         Underground Garage ATTN: Mark / Shawn")
        text_object.textLine(text="")
        my_canvas.drawText(text_object)
        my_canvas.showPage()

    my_canvas.save()


if __name__ == '__main__'
    textobject_demo()

有没有更好的方法?

我正在尝试使用 python 简化我们生成这些来源的方式,而不是每页复制和粘贴信息

感谢所有帮助

小吉弗

如果有人感兴趣,我就是这样做的:

from reportlab.pdfgen import canvas
import pandas as pd
import datetime

po_numbers = []
l_in_in = []
w_in_in = []
h_in_in = []
alpha_letter = []
weights = []

data = pd.read_excel(r'/home/littlejiver/PycharmProjects/JSRFormGenerator/UOM conversions.xlsx')
df = pd.DataFrame(data, columns=['Order', 'L (in)', 'W (in)', 'H (in)', 'Weight (lbs)', 'Letter'])

alpha_or_num = input("Letters or Numbers (L or N)?: ")

for i in df["Order"].dropna():
    po_numbers.append(str(i).replace(".0", ""))
for i in df["L (in)"].dropna():
    l_in_in.append(i)
for i in df["W (in)"].dropna():
    w_in_in.append(i)
for i in df["H (in)"].dropna():
    h_in_in.append(i)
for i in df["Letter"].dropna():
    alpha_letter.append(i)
    print(i)
for i in df["Weight (lbs)"].dropna():
    weights.append(i)

number_of_pieces = len(df[df.Order.notnull()])

print(number_of_pieces)
iir_number = "IIR " + input("Enter IIR Number (XX-XX): ")
file_name = iir_number + " Shipping Labels.pdf"
document_title = iir_number + " Shipping Labels.pdf"
title = "To: Voisey's Bay Mine Site – EXPANSION PROJECT (VBME)"
ship_to_address_line_1 = "C/o Innu Mikun Hangar"
ship_to_address_line_2 = "Happy Valley-Goose Bay,"
year = datetime.date.today().year
vbme = input("Enter VMBE # (XXXX):")
vbme_number = "CC0012 - VBME-" + str(year)[-2:] + "-" + vbme + " " + iir_number
piece_number = " of " + str(number_of_pieces)
special_storage_instruction = "No"
attn_name_line_1 = "IIR – REDPATH Canada Limited"
attn_name_line_2 = "Underground Garage ATTN: Mark / Shawn"

pdf = canvas.Canvas(file_name)
pdf.setTitle(document_title)


def draw_my_ruler(pdf):
    pdf.drawString(100, 810, 'x100')
    pdf.drawString(200, 810, 'x200')
    pdf.drawString(300, 810, 'x300')
    pdf.drawString(400, 810, 'x400')
    pdf.drawString(500, 810, 'x500')
    pdf.drawString(600, 810, 'x600')
    pdf.drawString(700, 810, 'x700')
    pdf.drawString(800, 810, 'x800')

    pdf.drawString(10, 100, 'y100')
    pdf.drawString(10, 200, 'x200')
    pdf.drawString(10, 300, 'x300')
    pdf.drawString(10, 400, 'x400')
    pdf.drawString(10, 500, 'x500')
    pdf.drawString(10, 600, 'x600')
    pdf.drawString(10, 700, 'x700')
    pdf.drawString(10, 800, 'x800')


def create_label_page(piece, po_number, length, width, height, aletter, weight):
    pdf.setFont('Times-Bold', 17)
    pdf.drawCentredString(300, 720, title)

    pdf.setFont('Times-Bold', 14)
    pdf.drawString(150, 680, ship_to_address_line_1)
    pdf.drawString(150, 665, ship_to_address_line_2)
    pdf.drawString(150, 650, ship_to_address_line_3)

    pdf.setFont('Times-Roman', 13)
    pdf.drawString(175, 600, vbme_number)
    pdf.drawString(175, 575, "Product Description: " + po_number)
    if alpha_or_num == "L":
        pdf.drawString(175, 550, "Piece: " + str(piece) + " of " + str(number_of_pieces) + " (" + aletter + ")")
    else:
        pdf.drawString(175, 550, "Piece: " + str(piece) + " of " + str(number_of_pieces))
    pdf.drawString(175, 550, "Piece: " + str(piece) + " of " + str(number_of_pieces))
    pdf.drawString(175, 525, "L " + str(length) + " x W " + str(width) + " x H " + str(height) + '"')
    pdf.drawString(175, 500, "Gross Weight: " + str(weight) + " Lbs")
    pdf.drawString(175, 475, "Net Weight: " + str(weight) + " Lbs")
    pdf.drawString(150, 375, "Special Storage Instructions: " + special_storage_instruction)
    pdf.setFont('Times-Bold', 13)
    pdf.drawString(175, 300, "Attn: " + attn_name_line_1)
    pdf.drawString(210, 285, attn_name_line_2)

    pdf.showPage()


for num_po in range(1, len(po_numbers) + 1):
    print(str(num_po) + " of " + str(len(po_numbers)))

    create_label_page(num_po, po_numbers[num_po - 1], l_in_in[num_po - 1], w_in_in[num_po - 1], h_in_in[num_po - 1],
                      alpha_letter[num_po - 1], weights[num_po - 1])

pdf.save()