# eksport_wizytówek.py -rw-r--r-- 2.2 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
"""Eksportuje hurtowo wizytówki z SVG do PDF na podstawie tabeli z danymi
"""

from dataclasses import dataclass
from os import makedirs
from os.path import join as join_path
import subprocess

DATA_FILE = 'dane.csv'
MASTER_DESIGN = 'projekt_awers_v7_90x50_spad5.svg'
OUTPUT_DIR = 'eksport'
OUTPUT_DESIGN = 'awers_v7_{}_90x50_spad5.{}'
INKSCAPE_CMD = r"C:\Program Files\Inkscape\bin\inkscape.com"

def inkscape(input_svg: str, output_pdf: str) -> None:
    subprocess.run([
        INKSCAPE_CMD,
        '--export-type=pdf',
        f'--export-filename={output_pdf}',
        '--export-overwrite',
        '--export-area-drawing',
        '--pdf-font-strategy=draw-all',
        input_svg])

def customize(input_svg: str, output_svg: str, replacements: list[tuple[str, str]]) -> None:
    content = ""
    with open(input_svg, "r", encoding="utf8") as file_in:
        content = file_in.read()
    
    for old, new in replacements:
        content = content.replace(old, new)
    
    with open(output_svg, "w", encoding="utf8") as file_out:
        file_out.write(content)


def load_variants(input_csv: str) -> tuple[list[str], list[list[str]]]:
    columns = []
    variants = []
    with open(input_csv, "r", encoding="utf8") as file_in:
        line = file_in.readline().strip()
        for part in line.split(","):
            name = part.strip('"')
            columns.append(name)
        for line in file_in.readlines():
            variant = []
            line = line.strip()
            for part in line.split(","):
                value = part.strip('"')
                variant.append(value)
            variants.append(variant)
    
    return columns, variants


columns, variants = load_variants(DATA_FILE)

makedirs(OUTPUT_DIR, exist_ok=True)

inkscape(
    join_path('projekt_rewers_v7_90x50_spad5.svg'),
    join_path(OUTPUT_DIR, 'rewers_v7_90x50_spad5.pdf'),
)

for variant in variants:
    custom_design = join_path(OUTPUT_DIR, OUTPUT_DESIGN.format(variant[0], "svg"))
    custom_export = join_path(OUTPUT_DIR, OUTPUT_DESIGN.format(variant[0], "pdf"))
    customize(MASTER_DESIGN, custom_design, zip(columns[1:], variant[1:]))
    inkscape(custom_design, custom_export)