qr generator working properly well
This commit is contained in:
7
qr_script/qr_file.csv
Normal file
7
qr_script/qr_file.csv
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
qr,nazov
|
||||||
|
123123141412313000,qr1
|
||||||
|
324503450345839,qr2
|
||||||
|
234252342424,qr3
|
||||||
|
23425252342424,qr4
|
||||||
|
111111111,qr5
|
||||||
|
0,qr6
|
||||||
|
BIN
qr_script/qr_file.ods
Normal file
BIN
qr_script/qr_file.ods
Normal file
Binary file not shown.
113
qr_script/qr_generator.py
Normal file
113
qr_script/qr_generator.py
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
QR Code Generator
|
||||||
|
|
||||||
|
Reads a CSV/TSV file where:
|
||||||
|
- First row is a header (skipped).
|
||||||
|
- First column contains the data/text to encode into a QR code.
|
||||||
|
- Second column contains the base name for the output PNG file.
|
||||||
|
|
||||||
|
Uses the `qrencode` command-line tool to generate QR codes.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import csv
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Generate QR codes from a data file using qrencode."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"input_file",
|
||||||
|
help="Path to the input file (CSV or TSV). The first row is treated as a header and skipped.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-d",
|
||||||
|
"--delimiter",
|
||||||
|
default=None,
|
||||||
|
help="Column delimiter (default: auto-detect — comma for .csv, tab otherwise).",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-o",
|
||||||
|
"--output-dir",
|
||||||
|
default=".",
|
||||||
|
help="Directory to write PNG files into (default: current directory).",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--qrencode",
|
||||||
|
default="qrencode",
|
||||||
|
help="Path to the qrencode binary (default: qrencode).",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--size",
|
||||||
|
type=int,
|
||||||
|
default=10,
|
||||||
|
help="Module size in pixels (qrencode -s, default: 10).",
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# --- auto-detect delimiter ---
|
||||||
|
if args.delimiter is None:
|
||||||
|
if args.input_file.lower().endswith(".csv"):
|
||||||
|
args.delimiter = ","
|
||||||
|
else:
|
||||||
|
args.delimiter = "\t"
|
||||||
|
|
||||||
|
# --- check qrencode ---
|
||||||
|
try:
|
||||||
|
subprocess.run(
|
||||||
|
[args.qrencode, "--version"],
|
||||||
|
capture_output=True,
|
||||||
|
check=True,
|
||||||
|
)
|
||||||
|
except (FileNotFoundError, subprocess.CalledProcessError):
|
||||||
|
print(
|
||||||
|
f"Error: '{args.qrencode}' not found or not working. Install qrencode (e.g. apt install qrencode).",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
os.makedirs(args.output_dir, exist_ok=True)
|
||||||
|
|
||||||
|
# --- read file and generate QR codes ---
|
||||||
|
with open(args.input_file, "r", encoding="utf-8") as fh:
|
||||||
|
reader = csv.reader(fh, delimiter=args.delimiter)
|
||||||
|
header = next(reader) # skip header row
|
||||||
|
print(f"Skipped header: {header}")
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
for row_num, row in enumerate(reader, start=2): # row 2 = first data row
|
||||||
|
if not row or len(row) < 2:
|
||||||
|
print(f"Row {row_num}: skipping (needs at least 2 columns)", file=sys.stderr)
|
||||||
|
continue
|
||||||
|
|
||||||
|
data = row[0].strip()
|
||||||
|
name = row[1].strip()
|
||||||
|
|
||||||
|
if not data or not name:
|
||||||
|
print(f"Row {row_num}: skipping (empty column)", file=sys.stderr)
|
||||||
|
continue
|
||||||
|
|
||||||
|
output_path = os.path.join(args.output_dir, f"{name}.png")
|
||||||
|
|
||||||
|
cmd = [
|
||||||
|
args.qrencode,
|
||||||
|
"-s", str(args.size),
|
||||||
|
"-o", output_path,
|
||||||
|
data,
|
||||||
|
]
|
||||||
|
|
||||||
|
print(f"Row {row_num}: encoding '{data}' -> {output_path}")
|
||||||
|
subprocess.run(cmd, check=True)
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
print(f"\nDone. Generated {count} QR code(s) in '{args.output_dir}'.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
BIN
qr_script/qr_output/qr1.png
Normal file
BIN
qr_script/qr_output/qr1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 376 B |
BIN
qr_script/qr_output/qr2.png
Normal file
BIN
qr_script/qr_output/qr2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 379 B |
BIN
qr_script/qr_output/qr3.png
Normal file
BIN
qr_script/qr_output/qr3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 374 B |
BIN
qr_script/qr_output/qr4.png
Normal file
BIN
qr_script/qr_output/qr4.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 372 B |
BIN
qr_script/qr_output/qr5.png
Normal file
BIN
qr_script/qr_output/qr5.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 375 B |
BIN
qr_script/qr_output/qr6.png
Normal file
BIN
qr_script/qr_output/qr6.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 382 B |
Reference in New Issue
Block a user