Add new CLI command for file name extraction and hash generation
- Implemented `get_filenames` command in `batch_extractor.py` to extract file names from a specified directory and generate a dated hash for each file. - Updated README.md with instructions for using the new command. - Enhanced PDF generation in `build_pdf.py` by modifying file naming conventions to include article and size. - Added example PowerShell script for batch processing tasks. - Introduced new example Excel file for user reference.
This commit is contained in:
41
build_pdf.py
41
build_pdf.py
@@ -17,6 +17,7 @@ from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT
|
||||
from read_image import read_datamatrix_zxing
|
||||
import render_eps
|
||||
import treepoem
|
||||
import html
|
||||
|
||||
import click
|
||||
from pathlib import Path
|
||||
@@ -184,7 +185,7 @@ def place_text(c: canvas.Canvas, text: str, font_name: str,
|
||||
|
||||
while True:
|
||||
# Заменяем явные переносы на HTML-тег для ReportLab
|
||||
text_for_pdf = current_text.replace('\n', '<br/>')
|
||||
text_for_pdf = html.escape(current_text)
|
||||
p = Paragraph(text_for_pdf, style)
|
||||
|
||||
# Вычисляем реальные размеры
|
||||
@@ -262,13 +263,13 @@ def draw_label_page(c: canvas.Canvas, font_name: str, dm_image: ImageReader, lab
|
||||
eac_image_y = CHZ_image_y
|
||||
c.drawImage(load_png('resources/eac-conformity-mark-seeklogo.png'), eac_image_x, eac_image_y, width=eac_image_h, height=CHZ_image_h, mask='auto')
|
||||
|
||||
page_num_h = 2 * mm
|
||||
page_num_h = 3 * mm
|
||||
page_num_y = CHZ_image_y - page_num_h - 0.3 * mm
|
||||
|
||||
place_text(c, f'№ {label_data.page_num}', font_name,
|
||||
x=img_x, y=page_num_y, width=10 * mm, height=page_num_h,
|
||||
font_size=8, font_min = 3, font_type='bold', align='center')
|
||||
#c.drawBoundary(c, img_x, page_num_y, width=10 * mm, height=page_num_h)
|
||||
x=img_x, y=page_num_y, width=20 * mm, height=page_num_h,
|
||||
font_size=8, font_min = 3, font_type='bold', align='left')
|
||||
#c.drawBoundary(c, img_x, page_num_y, width=20 * mm, height=page_num_h)
|
||||
|
||||
# Ниже на левой панели у вас останется место под логотипы ЧЗ, EAC и №1
|
||||
# ... (резерв места от y=0 до y=10мм)
|
||||
@@ -295,28 +296,28 @@ def draw_label_page(c: canvas.Canvas, font_name: str, dm_image: ImageReader, lab
|
||||
ean_barcode_h += 0.5
|
||||
|
||||
# 5. Артикул (одна строка, жирно)
|
||||
art_h = 2 * mm
|
||||
art_h = 3 * mm
|
||||
art_y = ean_barcode_y + ean_barcode_h
|
||||
place_text(c, f"арт.: {label_data.article}", font_name,
|
||||
x=right_x, y=art_y, width=right_w, height=art_h,
|
||||
font_size=5, font_type='bold', align='center')
|
||||
font_size=7, font_type='bold', align='center')
|
||||
|
||||
# 4. Размер (одна строка, жирно)
|
||||
size_h = 2 * mm
|
||||
size_h = 3 * mm
|
||||
size_y = art_y + art_h
|
||||
place_text(c, f"размер: {label_data.size}", font_name,
|
||||
x=right_x, y=size_y, width=right_w, height=size_h,
|
||||
font_size=5, font_type='bold', align='center')
|
||||
font_size=7, font_type='bold', align='center')
|
||||
|
||||
# 3. Цвет (одна строка, жирно)
|
||||
color_h = 2 * mm
|
||||
color_h = 3 * mm
|
||||
color_y = size_y + size_h
|
||||
place_text(c, f"цвет: {label_data.color}", font_name,
|
||||
x=right_x, y=color_y, width=right_w, height=color_h,
|
||||
font_size=5, font_type='bold', align='center')
|
||||
font_size=7, font_type='bold', align='center')
|
||||
|
||||
# 2. Описание (Крупно, жирно, центр, занимает всё среднее пространство)
|
||||
desc_h = 16 * mm
|
||||
desc_h = 13 * mm
|
||||
desc_y = color_y + color_h
|
||||
place_text(c, f"{label_data.description}", font_name,
|
||||
x=right_x, y=desc_y, width=right_w, height=desc_h,
|
||||
@@ -360,6 +361,7 @@ def process_batch(base64_codes: list[str], excel_path: str, output_dir: str):
|
||||
print(f"Ошибка парсинга кода {b64[:10]}... : {e}")
|
||||
|
||||
font_name = init_pdf_font()
|
||||
total_files = len(grouped_codes)
|
||||
|
||||
# 3. Оркестрация: 1 GTIN = 1 Многостраничный PDF файл
|
||||
for search_key, raw_bytes_list in grouped_codes.items():
|
||||
@@ -379,7 +381,7 @@ def process_batch(base64_codes: list[str], excel_path: str, output_dir: str):
|
||||
label_data = LabelData.from_excel_row(target_row)
|
||||
|
||||
# 3.2. Создаем холст PDF для группы
|
||||
pdf_path = os.path.join(output_dir, f"Labels_{search_key}.pdf")
|
||||
pdf_path = os.path.join(output_dir, f"{label_data.article}_{label_data.size}_{search_key}.pdf")
|
||||
# Размер страницы 58х40 мм
|
||||
c = canvas.Canvas(pdf_path, pagesize=(58*mm, 40*mm))
|
||||
|
||||
@@ -398,9 +400,13 @@ def process_batch(base64_codes: list[str], excel_path: str, output_dir: str):
|
||||
# Рисуем страницу, передавая ПОЛНЫЙ КОД МАРКИРОВКИ вместо короткого search_key
|
||||
draw_label_page(c, font_name, dm_image, label_data, readable_km)
|
||||
|
||||
print(' ', end='\r')
|
||||
print(f'Обработана страница {label_data.page_num} из {len(raw_bytes_list)}', end='\r')
|
||||
|
||||
# Сохраняем и закрываем сгенерированный PDF документ
|
||||
c.save()
|
||||
print(f" -> Сохранен файл: {pdf_path}")
|
||||
total_files -= 1
|
||||
click.echo(f" -> Сохранен файл: {pdf_path} \n Осталось {total_files}")
|
||||
|
||||
|
||||
@click.command(help="Генерирует PDF-этикетки на основе извлеченных Base64-кодов и Excel-шаблона.")
|
||||
@@ -455,4 +461,9 @@ def cli(codes_xlsx: Path, template_xlsx: Path, output_dir: Path):
|
||||
click.secho(f"Критическая ошибка при генерации PDF: {e}", fg="red")
|
||||
|
||||
if __name__ == "__main__":
|
||||
cli()
|
||||
cli()
|
||||
# Left priamry for testing reasons!
|
||||
# test_str = Path('c:/Python/CRPT/LabelExtractor/data/штучка_на_печать.xlsx')
|
||||
# template_path = Path('c:/Python/CRPT/LabelExtractor/data/Шаблон для загрузки этикеток.xlsx')
|
||||
# output_path = Path('c:/Python/CRPT/LabelExtractor/data/output/units')
|
||||
# cli(test_str,template_path, output_path)
|
||||
Reference in New Issue
Block a user