Skip to content

Split premades

split_pdf(dry_run=True, roles=['Defender', 'Caster', 'Support', 'Martial'], level_max=3, pdf_path=Path(glob('./automation/_input/*PremadeSheet*pdf')[0]), out_folder=Path('../docs/src/1_Mechanics/PremadeCharacters/'), return_paths=False)

Splits PDF for premades into individual png files: Premade_{Role}_Level{#}.png

Parameters:

Name Type Description Default
dry_run bool

If true (default), just logs filename as info.

True
roles list

Roles in order as they appear in pdf. Defaults to ["Defender", "Caster", "Support", "Martial"].

['Defender', 'Caster', 'Support', 'Martial']
level_max int

Max levels being split in file. Defaults to 3.

3
pdf_path Path

Input pdf path. Defaults to Path(glob("../_input/temp_CharSheet*pdf")[0]).

Path(glob('./automation/_input/*PremadeSheet*pdf')[0])
out_folder Path

Output png folder. Defaults to Path("../docs/src/1_Mechanics/PremadeCharacters/").

Path('../docs/src/1_Mechanics/PremadeCharacters/')
return_paths bool

If True, function returns list of output Pathlib objects. Defaults False.

False
Source code in automation/pdf/split_premades.py
 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
def split_pdf(
    dry_run: bool = True,
    roles: list = ["Defender", "Caster", "Support", "Martial"],
    level_max: int = 3,
    pdf_path: Path = Path(glob("./automation/_input/*PremadeSheet*pdf")[0]),
    out_folder: Path = Path("../docs/src/1_Mechanics/PremadeCharacters/"),
    return_paths: bool = False,
):
    """Splits PDF for premades into individual png files: Premade_{Role}_Level{#}.png

    Args:
        dry_run (bool, optional): If true (default), just logs filename as info.
        roles (list, optional): Roles in order as they appear in pdf.
            Defaults to ["Defender", "Caster", "Support", "Martial"].
        level_max (int, optional): Max levels being split in file. Defaults to 3.
        pdf_path (Path, optional): Input pdf path.
            Defaults to Path(glob("../_input/temp_CharSheet*pdf")[0]).
        out_folder (Path, optional): Output png folder.
            Defaults to Path("../docs/src/1_Mechanics/PremadeCharacters/").
        return_paths (bool): If True, function returns list of output Pathlib objects.
            Defaults False.
    """
    role_idx = 0  # Initialize which role
    level = 0  # Initialize which level for role
    pages = convert_from_path(pdf_path, 500)
    output_paths = []
    for num, page in enumerate(pages):
        level = num % 3 + 1
        file_name = "Premade_" + roles[role_idx] + "_Level" + str(level) + ".png"

        logger.info(file_name)

        fp = out_folder / file_name
        output_paths.append(fp)

        if not dry_run:
            page.save(fp, "PNG")
        if level == level_max:
            role_idx += 1

    if return_paths:
        return output_paths