добавлено содержание
This commit is contained in:
parent
69a38caa79
commit
758cf53f93
12
pdf_merge.py
12
pdf_merge.py
@ -92,6 +92,7 @@ def merge(config_path: Path) -> None:
|
|||||||
|
|
||||||
writer = PdfWriter()
|
writer = PdfWriter()
|
||||||
total_added = 0
|
total_added = 0
|
||||||
|
bookmarks: list[tuple[str, int]] = [] # (label, 0-based page index в результате)
|
||||||
|
|
||||||
for i, step in enumerate(steps, 1):
|
for i, step in enumerate(steps, 1):
|
||||||
if "file" not in step:
|
if "file" not in step:
|
||||||
@ -113,10 +114,13 @@ def merge(config_path: Path) -> None:
|
|||||||
print(f"Шаг {i} ({step['file']}): {e}", file=sys.stderr)
|
print(f"Шаг {i} ({step['file']}): {e}", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
label = step.get("label", "")
|
||||||
|
if label:
|
||||||
|
bookmarks.append((label, total_added))
|
||||||
|
|
||||||
for idx in indices:
|
for idx in indices:
|
||||||
writer.add_page(reader.pages[idx])
|
writer.add_page(reader.pages[idx])
|
||||||
|
|
||||||
label = step.get("label", "")
|
|
||||||
label_str = f" [{label}]" if label else ""
|
label_str = f" [{label}]" if label else ""
|
||||||
print(
|
print(
|
||||||
f" + {step['file']}{label_str}: "
|
f" + {step['file']}{label_str}: "
|
||||||
@ -125,6 +129,12 @@ def merge(config_path: Path) -> None:
|
|||||||
)
|
)
|
||||||
total_added += len(indices)
|
total_added += len(indices)
|
||||||
|
|
||||||
|
# Добавляем закладки (содержание в боковой панели PDF-ридера)
|
||||||
|
if bookmarks:
|
||||||
|
for bm_label, page_idx in bookmarks:
|
||||||
|
writer.add_outline_item(bm_label, page_idx)
|
||||||
|
print(f"\n Содержание: {len(bookmarks)} закладок")
|
||||||
|
|
||||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
with open(output_path, "wb") as out:
|
with open(output_path, "wb") as out:
|
||||||
writer.write(out)
|
writer.write(out)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user