import os, json from google.oauth2.credentials import Credentials from googleapiclient.discovery import build token_path = os.path.expanduser("~/.hermes/profiles/reviewer/google_token.json") with open(token_path, "r") as f: creds_data = json.load(f) creds = Credentials.from_authorized_user_info(creds_data) docs_service = build("docs", "v1", credentials=creds) doc_id = "1sjTe7Im51CY2Tl0SNQrm8xmO7bYweMkq44E0WXaxs1I" doc = docs_service.documents().get(documentId=doc_id).execute() def scan_elements(elements): for el in elements: if "paragraph" in el: for p_el in el["paragraph"]["elements"]: pos_ids = p_el.get("positionedObjectIds", []) if pos_ids: txt = "".join([e.get("textRun", {}).get("content", "") for e in el["paragraph"]["elements"] if "textRun" in e]) print(f"Found pos_ids {pos_ids} at paragraph startIndex {el['startIndex']}: text = {repr(txt)}") elif "table" in el: for row in el["table"]["tableRows"]: for cell in row["tableCells"]: scan_elements(cell.get("content", [])) scan_elements(doc.get("body", {}).get("content", []))