agrego el script encargado de reemplazar palabras claves dentro del documento
This commit is contained in:
142
reemplazar_variables_contrato.py
Normal file
142
reemplazar_variables_contrato.py
Normal file
@@ -0,0 +1,142 @@
|
||||
from google.auth.transport.requests import Request
|
||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
||||
from google.oauth2.credentials import Credentials
|
||||
from google.oauth2 import service_account
|
||||
from googleapiclient.discovery import build
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
|
||||
global Request, InstalledAppFlow, Credentials, service_account, build, os, sys, json, LISTA_BUSCAR_REEMPLAZAR, base_dir, libs_dir, SCOPES, CREDENTIALS_PATH, TOKEN_PATH, _load_json, get_google_services, replace_vars_doc, drive_service, docs_service, gdoc_id, lista_oficial_reemplazar, success
|
||||
|
||||
# Lista oficial de variables a reemplazar
|
||||
LISTA_BUSCAR_REEMPLAZAR = {list_buscar}
|
||||
|
||||
|
||||
# ===== Rocketbot-compatible py_libs path =====
|
||||
base_dir = os.path.dirname(sys.executable) if getattr(
|
||||
sys, 'frozen', False) else os.getcwd()
|
||||
libs_dir = os.path.join(base_dir, 'librerias', 'py_libs', 'py310')
|
||||
if os.path.isdir(libs_dir) and libs_dir not in sys.path:
|
||||
sys.path.insert(0, libs_dir)
|
||||
|
||||
|
||||
# SCOPES actualizados (solo Drive y Docs)
|
||||
SCOPES = {scopes_api_google}
|
||||
|
||||
|
||||
CREDENTIALS_PATH = '{gdoc_sa_json}'
|
||||
TOKEN_PATH = '{gdoc_token_json}'
|
||||
|
||||
|
||||
def _load_json(path):
|
||||
with open(path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
def get_google_services(credentials_json_path=CREDENTIALS_PATH, token_json_path=TOKEN_PATH):
|
||||
info = _load_json(credentials_json_path)
|
||||
|
||||
# Service account
|
||||
if isinstance(info, dict) and info.get('type') == 'service_account':
|
||||
creds = service_account.Credentials.from_service_account_file(
|
||||
credentials_json_path, scopes=SCOPES)
|
||||
drive_service = build(
|
||||
'drive', 'v3', credentials=creds, cache_discovery=False)
|
||||
docs_service = build(
|
||||
'docs', 'v1', credentials=creds, cache_discovery=False)
|
||||
return drive_service, docs_service
|
||||
|
||||
# OAuth flow
|
||||
creds = None
|
||||
if os.path.exists(token_json_path):
|
||||
creds = Credentials.from_authorized_user_file(token_json_path, SCOPES)
|
||||
|
||||
if not creds or not creds.valid:
|
||||
if creds and creds.expired and creds.refresh_token:
|
||||
creds.refresh(Request())
|
||||
else:
|
||||
flow = InstalledAppFlow.from_client_secrets_file(
|
||||
credentials_json_path, SCOPES)
|
||||
try:
|
||||
creds = flow.run_local_server(port=0)
|
||||
except Exception:
|
||||
creds = flow.run_console()
|
||||
|
||||
os.makedirs(os.path.dirname(token_json_path), exist_ok=True)
|
||||
with open(token_json_path, 'w', encoding='utf-8') as f:
|
||||
f.write(creds.to_json())
|
||||
|
||||
drive_service = build(
|
||||
'drive', 'v3', credentials=creds, cache_discovery=False)
|
||||
docs_service = build(
|
||||
'docs', 'v1', credentials=creds, cache_discovery=False)
|
||||
|
||||
return drive_service, docs_service
|
||||
|
||||
|
||||
def replace_vars_doc(docs_service, gdoc_id, replacements_values):
|
||||
"""
|
||||
Reemplaza las variables en el documento de Google Docs.
|
||||
Recibe el gdoc_id y un array de valores en el mismo orden que LISTA_BUSCAR_REEMPLAZAR.
|
||||
"""
|
||||
if not replacements_values:
|
||||
print("No se proporcionaron valores para reemplazar.")
|
||||
return False
|
||||
|
||||
requests = []
|
||||
|
||||
# Iterar sobre las variables oficiales y construir la lista de reemplazo
|
||||
for i in range(min(len(LISTA_BUSCAR_REEMPLAZAR), len(replacements_values))):
|
||||
key = LISTA_BUSCAR_REEMPLAZAR[i]
|
||||
value = str(replacements_values[i]) if replacements_values[i] is not None else ""
|
||||
print('Buscando variable a remplazar:', key)
|
||||
print('Remplazando por:', value)
|
||||
|
||||
requests.append({
|
||||
'replaceAllText': {
|
||||
'containsText': {
|
||||
'text': key,
|
||||
'matchCase': True
|
||||
},
|
||||
'replaceText': value
|
||||
}
|
||||
})
|
||||
|
||||
if requests:
|
||||
print(f"Enviando {len(requests)} solicitudes de reemplazo al documento {gdoc_id}")
|
||||
docs_service.documents().batchUpdate(
|
||||
documentId=gdoc_id, body={'requests': requests}).execute()
|
||||
print("Reemplazo completado exitosamente.")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
# Iniciar servicios (Solo Drive y Docs)
|
||||
drive_service, docs_service = get_google_services(CREDENTIALS_PATH, TOKEN_PATH)
|
||||
|
||||
# Variables de entrada (esto usualmente viene de Rocketbot via GetVar)
|
||||
gdoc_id = '{gdoc_id}'
|
||||
lista_oficial_reemplazar = {lista_oficial_reemplazar}
|
||||
|
||||
|
||||
try:
|
||||
# Ejecutar reemplazo si se cuenta con la información
|
||||
if gdoc_id and lista_oficial_reemplazar:
|
||||
success = replace_vars_doc(docs_service, gdoc_id, lista_oficial_reemplazar)
|
||||
try:
|
||||
SetVar('res_reemplazo_variables', success)
|
||||
except NameError:
|
||||
pass
|
||||
else:
|
||||
print("Falta gdoc_id o lista_oficial_reemplazar para ejecutar el reemplazo.")
|
||||
|
||||
except Exception as e:
|
||||
import traceback
|
||||
print(f"Error en el reemplazo de variables: {str(e)}")
|
||||
print(traceback.format_exc())
|
||||
try:
|
||||
SetVar('error', {'error': 'Error al remplazar las variables en Google Docs', 'descripcion': str(e)})
|
||||
except NameError:
|
||||
pass
|
||||
Reference in New Issue
Block a user