Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit a6b75fc

Browse files
feat: add convert_document_to_annotate_file_json (#124)
* feat: add convert_document_to_annotate_file_json * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 70106c1 commit a6b75fc

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

google/cloud/documentai_toolbox/wrappers/document.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,19 @@ def convert_document_to_annotate_file_response(self) -> AnnotateFileResponse:
732732
"""
733733
return _convert_to_vision_annotate_file_response(self.text, self.pages)
734734

735+
def convert_document_to_annotate_file_json_response(self) -> str:
736+
r"""Convert OCR data from Document.proto to JSON str of AnnotateFileResponse for Vision API.
737+
738+
Args:
739+
None.
740+
Returns:
741+
str:
742+
JSON string of TextAnnotations.
743+
"""
744+
return AnnotateFileResponse.to_json(
745+
_convert_to_vision_annotate_file_response(self.text, self.pages)
746+
)
747+
735748
def export_images(
736749
self, output_path: str, output_file_prefix: str, output_file_extension: str
737750
) -> List[str]:

tests/unit/test_document.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,19 @@ def test_convert_document_to_annotate_file_response():
536536
assert actual == expected
537537

538538

539+
def test_convert_document_to_annotate_file_json_response():
540+
doc = document.Document.from_document_path(
541+
document_path="tests/unit/resources/0/toolbox_invoice_test-0.json"
542+
)
543+
544+
actual = doc.convert_document_to_annotate_file_json_response()
545+
546+
with open("tests/unit/resources/toolbox_invoice_test-0-vision.json", "r") as f:
547+
expected = f.read()
548+
549+
assert actual == expected
550+
551+
539552
def test_export_images(get_bytes_images_mock):
540553
doc = document.Document.from_gcs(
541554
gcs_bucket_name="test-directory", gcs_prefix="documentai/output/123456789/0"

0 commit comments

Comments
 (0)