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"] requests = [] border_style = { "color": {"color": {"rgbColor": {"red": 0.4, "green": 0.4, "blue": 0.4}}}, "width": {"magnitude": 1, "unit": "PT"}, "dashStyle": "DASH" } # 1. Style for photo cell (row 0, col 0) 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": 25, "unit": "PT"}, "paddingBottom": {"magnitude": 25, "unit": "PT"}, "paddingLeft": {"magnitude": 10, "unit": "PT"}, "paddingRight": {"magnitude": 10, "unit": "PT"} }, "fields": "borderTop,borderBottom,borderLeft,borderRight,contentAlignment,paddingTop,paddingBottom,paddingLeft,paddingRight" } }) # 2. Fix text alignment for Cell 1 (Header): Center alignment cell_1_content = table_el["table"]["tableRows"][0]["tableCells"][1]["content"] for c in cell_1_content: if "paragraph" in c: p_st = c["startIndex"] p_en = c["endIndex"] requests.append({ "updateParagraphStyle": { "range": {"startIndex": p_st, "endIndex": p_en}, "paragraphStyle": { "alignment": "CENTER" }, "fields": "alignment" } }) 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_page_{page_idx+1}.png") print(f"Saved page {page_idx+1} preview to /tmp/final_page_{page_idx+1}.png")