我有一个 python 代码,可以将“Claims New”选项卡打印到标签打印机。我的问题是,我能弄清楚打印内容的唯一方法是将所有内容都放在一个单元格中(如图所示)。我想要一个 QR 码也包含在我的 PDF 导出中。我似乎无法弄清楚如何调整此代码以便打印两个单元格(A1 和 A12)。不幸的是,我也无法将 QR 码与其他文本连接起来(使其成为一个代码)。
当前输出的是不包含二维码的PDF(只是A1单元格)。我想要的输出包括两个单元格(所以 QR 码在那里)。代码没有回溯,它运行它只是不包括所有需要的数据。
有什么想法吗?
代码:
import os
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import win32print
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import landscape, portrait
from reportlab.lib.units import mm
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
import time
import qr
# Set up Google Sheets API credentials
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('creds.json', scope)
client = gspread.authorize(creds)
# Set up printer name and font
printer_name = "Rollo Printer"
font_path = "arial.ttf"
# Register Arial font
pdfmetrics.registerFont(TTFont('Arial', font_path))
# Open the Google Sheet and select Label and Claims worksheets
sheet_url = "url"
label_sheet = client.open_by_url(sheet_url).worksheet("Label")
claims_sheet = client.open_by_url(sheet_url).worksheet("Claims New")
# Get the number of label copies to print
num_copies = int(label_sheet.acell("F2").value)
# Set up label filename and PDF canvas
label_file = "label.pdf"
label_canvas = canvas.Canvas(label_file, pagesize=landscape((400, 250)),bottomup=432)
# Write label text to PDF canvas
label_text = label_sheet.get_all_values()
x_pos = 10 # set initial x position
y_pos = 260 # set initial y position
line_height = 20 # set line height for text
label_canvas.setFont('Arial', 20)
for row in label_text:
for col in row:
textobject = label_canvas.beginText()
textobject.setFont('Arial', 20)
textobject.setTextOrigin(x_pos, y_pos)
lines = col.split("\n")
for line in lines:
textobject.textLine(line)
y_pos -= line_height
label_canvas.drawText(textobject)
x_pos += 145
y_pos = 260
x_pos = 10
# Save the label PDF and print to the printer
label_canvas.save()
for i in range(num_copies):
time.sleep(2)
os.startfile(label_file, 'printto', printer_name, "", 1)
# Set up claims filename and PDF canvas
claims_file = "claims.pdf"
claims_canvas = canvas.Canvas(claims_file, pagesize=landscape((400, 250)),bottomup=432)
# Write claims text to PDF canvas
claims_text = claims_sheet.get_all_values()
x_pos = 10
y_pos = 260 # set initial y position
line_height = 20
claims_canvas.setFont('Arial', 10)
for row in claims_text:
for col in row:
if col == claims_sheet.acell('A12').value:
qr_code = qr.QrCodeWidget(col)
bounds = qr_code.getBounds()
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
d = claims_canvas.translate(x_pos, y_pos)
claims_canvas.scale(1, -1)
qr_code.drawOn(claims_canvas, 0, 0)
claims_canvas.scale(1, -1)
claims_canvas.translate(-x_pos, -y_pos)
else:
textobject = claims_canvas.beginText()
textobject.setFont('Arial', 10)
textobject.setTextOrigin(x_pos, y_pos)
lines = col.split("\n")
for line in lines:
textobject.textLine(line)
y_pos -= line_height
claims_canvas.drawText(textobject)
x_pos += 145
y_pos = 260
x_pos = 10
# Save the claims PDF and print to the printer
time.sleep(1)
claims_canvas.save()
time.sleep(1)
os.startfile(claims_file, 'printto', printer_name, "",