SoFunction
Updated on 2024-12-18

Python to automatically complete the A4 label layout printing function

My wife asked me to help her to generate the passes of 100 people through Excel, and their school is planning to have a college entrance exam simulation. Due to the reform of the college entrance examination, each student's examination subject is different, and it needs to be automatically generated a bit.

One of my programmers rarely uses Excel on a regular basis and doesn't plan to delve into the software herself. How to solve her needs? I directly thought of python, the omnipotent python can surely take care of this small case.

Solutions

Data processing: this one's easy

Generate printable documents

This is a little difficult, I first thought of generating word. and python also has word package to solve, but then thought about it, this program has problems. word structure is not open source, formatting and styles should be handled with problems. In addition, word is not common in foreign countries, encountered a problem solution is certainly not much. PDF is different, PDF in foreign applications widely used by many people, certainly a good solution.

Find a Program

Through python to generate pdf program is determined, and indeed through Google to find the library pylabels.

How to solve

The first step is to install the library

pip install pylabels

Step 2 Execute the following code

import labels
from  import shapes
 
specs = (210, 297, 2, 8, 90, 25, corner_radius=2)
 
def draw_label(label, width, height, obj):
 
((2, 2, str(obj), fontName="Helvetica", fontSize=40))
 
# Create the sheet.
sheet = (specs, draw_label, border=True)
 
# Add a couple of labels.
sheet.add_label("Hello")
sheet.add_label("World")
 
# We can also add each item from an iterable.
sheet.add_labels(range(3, 22))
 
sheet.add_label("Oversized label here")
 
# Save the file and we are done.
('')
print("{0:d} label(s) output on {1:d} page(s).".format(sheet.label_count, sheet.page_count))

I won't share the data processing part, it's simply reading a csv file.

Above this Python to achieve automatic completion of the A4 label layout printing function is all that I have shared with you, I hope to give you a reference, and I hope that you will support me more.