From 41c954401574ec4b484019eb83b71b7975d0350d Mon Sep 17 00:00:00 2001 From: Linus Sehn <37184648+linozen@users.noreply.github.com> Date: Thu, 10 Aug 2023 20:12:52 +0200 Subject: [PATCH] Add `page_label` to JSON output (#81) The method `annot_to_dict` in json.py has also been refactored to remove keys with None values in the nested dictionary. This improves the clarity and reduces unnecessary data in the resulting dictionary. --------- Co-authored-by: Andrew Baumann --- pdfannots/printer/json.py | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/pdfannots/printer/json.py b/pdfannots/printer/json.py index fc0b2c5..7fc2bde 100644 --- a/pdfannots/printer/json.py +++ b/pdfannots/printer/json.py @@ -16,29 +16,18 @@ def annot_to_dict( result = { "type": annot.subtype.name, "page": annot.pos.page.pageno + 1, + "page_label": annot.pos.page.label, "start_xy": (annot.pos.x, annot.pos.y), + "prior_outline": getattr(doc.nearest_outline(annot.pos), 'title', None), + "text": annot.gettext(remove_hyphens), + "contents": annot.contents, + "author": annot.author, + "created": annot.created.strftime('%Y-%m-%dT%H:%M:%S') if annot.created else None, + "color": annot.color.ashex() if annot.color else None } - outline = doc.nearest_outline(annot.pos) - if outline: - result["prior_outline"] = outline.title - - if annot.text: - result['text'] = annot.gettext(remove_hyphens) - - if annot.contents: - result['contents'] = annot.contents - - if annot.author: - result['author'] = annot.author - - if annot.created: - result['created'] = annot.created.strftime('%Y-%m-%dT%H:%M:%S') - - if annot.color: - result['color'] = annot.color.ashex() - - return result + # Remove keys with None values in nested dictionary and return + return {k: v for k, v in result.items() if v is not None} class JsonPrinter(Printer):