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() body = doc.get("body", {}) content = body.get("content", []) table_el = content[2] table_start_index = table_el["startIndex"] # 4 cm = 113.4 pt width # 6 cm = 170.1 pt height # Total height = ~170 pt. With ~30pt text height inside, top+bottom padding = 70pt each (70 + 70 + 30 = 170pt). requests = [] # 1. Update Column 0 width to 113.4 pt (4 cm) requests.append({ "updateTableColumnProperties": { "tableStartLocation": {"index": table_start_index}, "columnIndices": [0], "tableColumnProperties": { "widthType": "FIXED_WIDTH", "width": {"magnitude": 113.4, "unit": "PT"} }, "fields": "widthType,width" } }) # Update Column 1 width to 397.88 pt (remaining usable width) requests.append({ "updateTableColumnProperties": { "tableStartLocation": {"index": table_start_index}, "columnIndices": [1], "tableColumnProperties": { "widthType": "FIXED_WIDTH", "width": {"magnitude": 397.88, "unit": "PT"} }, "fields": "widthType,width" } }) # 2. Update Cell 0 style for vertical portrait (4x6 cm ratio) border_style = { "color": {"color": {"rgbColor": {"red": 0.3, "green": 0.3, "blue": 0.3}}}, "width": {"magnitude": 1, "unit": "PT"}, "dashStyle": "DASH" } requests.append({ "updateTableCellStyle": { "tableRange": { "tableCellLocation": { "tableStartLocation": {"index": table_start_index}, "rowIndex": 0, "columnIndex": 0 }, "rowSpan": 1, "columnSpan": 1 }, "tableCellStyle": { "borderTop": border_style, "borderBottom": border_style, "borderLeft": border_style, "borderRight": border_style, "contentAlignment": "MIDDLE", "paddingTop": {"magnitude": 65, "unit": "PT"}, "paddingBottom": {"magnitude": 65, "unit": "PT"}, "paddingLeft": {"magnitude": 5, "unit": "PT"}, "paddingRight": {"magnitude": 5, "unit": "PT"} }, "fields": "borderTop,borderBottom,borderLeft,borderRight,contentAlignment,paddingTop,paddingBottom,paddingLeft,paddingRight" } }) print(f"Sending {len(requests)} requests...") resp = docs_service.documents().batchUpdate( documentId=doc_id, body={"requests": requests} ).execute() print("Updated successfully!") # Export PDF print("Exporting PDF...") request = drive_service.files().export_media(fileId=doc_id, mimeType="application/pdf") pdf_data = request.execute() output_dir = "/opt/ai-os/products/ceo/projects/GLV/reports" os.makedirs(output_dir, exist_ok=True) pdf_filename = "So_Yeu_Ly_Lich_Vo_Thi_Yen_Nhi_Hoan_Chinh.pdf" pdf_full_path = os.path.join(output_dir, pdf_filename) with open(pdf_full_path, "wb") as f: f.write(pdf_data) print(f"Saved completed PDF to {pdf_full_path}") pdf = pymupdf.open(stream=pdf_data, filetype="pdf") print(f"Total PDF pages: {len(pdf)}") for page_idx, page in enumerate(pdf): pix = page.get_pixmap(dpi=150) pix.save(f"/tmp/final_vertical_page_{page_idx+1}.png") print(f"Saved page {page_idx+1} preview to /tmp/final_vertical_page_{page_idx+1}.png")