import os, io, json from google.oauth2.credentials import Credentials from googleapiclient.discovery import build from googleapiclient.http import MediaIoBaseDownload import pymupdf token_path = os.path.expanduser("~/.hermes/profiles/reviewer/google_token.json") with open(token_path) as f: creds = Credentials.from_authorized_user_info(json.load(f)) docs_service = build("docs", "v1", credentials=creds) drive_service = build("drive", "v3", credentials=creds) doc_id = "1sjTe7Im51CY2Tl0SNQrm8xmO7bYweMkq44E0WXaxs1I" doc = docs_service.documents().get(documentId=doc_id).execute() positioned_objects = doc.get("positionedObjects", {}) print("Positioned objects to delete:", list(positioned_objects.keys())) requests = [] # Step 1: Delete floating positioned objects for obj_id in positioned_objects.keys(): requests.append({ "deletePositionedObject": { "objectId": obj_id } }) # Step 2: Update table column widths for table at element [2] (1x2 table) # Page width = 595.28pt, margins left+right = 42+42 = 84pt, usable = 511.28pt # Col 0 (photo box): 110pt # Col 1 (header): 401.28pt # Need to update via updateTableColumnProperties body = doc.get("body", {}) content = body.get("content", []) # Find table at index [2] -> startIndex 2 table_el = content[2] table_start_index = table_el["startIndex"] print(f"Table start index: {table_start_index}") requests.append({ "updateTableColumnProperties": { "tableStartLocation": {"index": table_start_index}, "columnIndices": [0], "tableColumnProperties": { "widthType": "FIXED_WIDTH", "width": {"magnitude": 110, "unit": "PT"} }, "fields": "widthType,width" } }) requests.append({ "updateTableColumnProperties": { "tableStartLocation": {"index": table_start_index}, "columnIndices": [1], "tableColumnProperties": { "widthType": "FIXED_WIDTH", "width": {"magnitude": 401.28, "unit": "PT"} }, "fields": "widthType,width" } }) # Step 3: Update the photo cell (Col 0) content - clear " x 6 cm" and insert proper text # Cell 0 content: startIndex=5, endIndex=13 (text " x 6 cm\n") # We want: "Ảnh\n4 x 6 cm" (center aligned) # First clear existing text, then insert table_cell_0 = table_el["table"]["tableRows"][0]["tableCells"][0] para_0 = table_cell_0["content"][0] st = para_0["startIndex"] en = para_0["endIndex"] print(f"Photo cell paragraph: startIndex={st}, endIndex={en}") # Delete existing content (keep newline at end) if en - 1 > st: requests.append({ "deleteContentRange": { "range": {"startIndex": st, "endIndex": en - 1} } }) # Insert proper text requests.append({ "insertText": { "location": {"index": st}, "text": "Ảnh\n4 x 6 cm" } }) # Format: center align, italic, small font requests.append({ "updateParagraphStyle": { "range": {"startIndex": st, "endIndex": st + 13}, # "Ảnh\n4 x 6 cm" "paragraphStyle": { "alignment": "CENTER" }, "fields": "alignment" } }) print(f"Total requests: {len(requests)}") print("Requests:", json.dumps([list(r.keys()) for r in requests], ensure_ascii=False)) resp = docs_service.documents().batchUpdate( documentId=doc_id, body={"requests": requests} ).execute() print("Done:", resp.get("replies")) # Export PDF print("Exporting PDF...") request = drive_service.files().export_media(fileId=doc_id, mimeType="application/pdf") pdf_data = request.execute() pdf_path = "/tmp/syyly_nhi_vo_final.pdf" with open(pdf_path, "wb") as f: f.write(pdf_data) pdf = pymupdf.open(stream=pdf_data, filetype="pdf") print(f"PDF pages: {len(pdf)}") p1 = pdf[0] pix = p1.get_pixmap(dpi=150) pix.save("/tmp/preview_fixed_p1.png") print("Saved preview to /tmp/preview_fixed_p1.png") # Print first few text blocks to verify layout blocks = p1.get_text("blocks") print("\nTop text blocks:") for b in blocks[:8]: print(f" Rect: ({b[0]:.1f}, {b[1]:.1f}, {b[2]:.1f}, {b[3]:.1f}) Text: {repr(b[4].strip()[:80])}")