From 794f81fa36199a48223b02061ed739c8b8acc01a Mon Sep 17 00:00:00 2001 From: linozen Date: Wed, 9 Aug 2023 19:02:47 +0200 Subject: [PATCH 1/3] feat(json): add `page_number` to output 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. --- pdfannots/printer/json.py | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/pdfannots/printer/json.py b/pdfannots/printer/json.py index fc0b2c5..ce225cc 100644 --- a/pdfannots/printer/json.py +++ b/pdfannots/printer/json.py @@ -15,28 +15,19 @@ def annot_to_dict( result = { "type": annot.subtype.name, - "page": annot.pos.page.pageno + 1, + "page_number" "number": annot.pos.page.pageno + 1, + "page_label": getattr(annot.pos.page, 'label', None), "start_xy": (annot.pos.x, annot.pos.y), + "prior_outline": getattr(doc.nearest_outline(annot.pos), 'title', None), + "text": annot.gettext(remove_hyphens) if annot.text else None, + "contents": getattr(annot, 'contents', None), + "author": getattr(annot, 'author', None), + "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() + # Remove keys with None values in nested dictionary + result = {k: v for k, v in result.items() if v is not None} return result From 7490cde4725f6e55fd7a4a9804a8cc063ca0e102 Mon Sep 17 00:00:00 2001 From: linozen Date: Thu, 10 Aug 2023 08:07:46 +0200 Subject: [PATCH 2/3] fix: remove unneccessary `getattr`s and return directly --- pdfannots/printer/json.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pdfannots/printer/json.py b/pdfannots/printer/json.py index ce225cc..382638d 100644 --- a/pdfannots/printer/json.py +++ b/pdfannots/printer/json.py @@ -15,21 +15,19 @@ def annot_to_dict( result = { "type": annot.subtype.name, - "page_number" "number": annot.pos.page.pageno + 1, - "page_label": getattr(annot.pos.page, 'label', None), + "page_number": 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) if annot.text else None, - "contents": getattr(annot, 'contents', None), - "author": getattr(annot, 'author', 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 } - # Remove keys with None values in nested dictionary - result = {k: v for k, v in result.items() if v is not None} - - 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): From d23f5bf9fff907f37b4c7c75a2598b43ae33f243 Mon Sep 17 00:00:00 2001 From: Andrew Baumann Date: Thu, 10 Aug 2023 20:07:30 +0200 Subject: [PATCH 3/3] avoid changing name of existing json field --- pdfannots/printer/json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdfannots/printer/json.py b/pdfannots/printer/json.py index 382638d..7fc2bde 100644 --- a/pdfannots/printer/json.py +++ b/pdfannots/printer/json.py @@ -15,7 +15,7 @@ def annot_to_dict( result = { "type": annot.subtype.name, - "page_number": annot.pos.page.pageno + 1, + "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),