0

When I run this code:

from tkinter import filedialog, simpledialog
import docx
import tkinter as tk
from datetime import date
import os


def main():
    
    try:
        # Crear Ventana Principal
        root = tk.Tk()
        root.withdraw()

        # Abrir cuadro de diálogo para seleccionar archivo Word
        filename = filedialog.askopenfilename(
            title="Seleccionar Archivo Word", filetypes=[("plantilla.docx", "*.docx")])

        if not filename:
            print("No se seleccionó ningún archivo.")
            return

        # Cerrar ventana de diálogo del archivo
        root.destroy()

        # Verificar si el archivo seleccionado es válido
        if not os.path.isfile(filename):
            print("El archivo seleccionado no es válido.")
            return

        # Abrir archivo Word seleccionado y guardar contenido en una variable
        document = docx.Document(filename)

        # Permitir al usuario ingresar campos
        senores = simpledialog.askstring("Input", "Ingresar los señores:")
        lugar = simpledialog.askstring("Input", "Ingresar el lugar:")
        fecha = date.today().strftime("%d/%m/%Y")
        actividades = {}
        for i in range(1, 7):
            actividad = simpledialog.askstring("Input", f"Ingrese la actividad {i}")
            actividades[f"actividad {i}"] = actividad
        resultados = {}
        for i in range(1, 6):
            resultado = simpledialog.askstring("Input", f"Ingrese el resultado {i}")
            resultados[f"resultado {i}"] = resultado
        observaciones = {}
        for i in range(1, 5):
            observacion = simpledialog.askstring("Input", f"Ingrese la observacion {i}")
            observaciones[f"observacion {i}"] = observacion

        # Buscar los párrafos que contienen los campos y reemplazar el texto
        for paragraph in document.paragraphs:
            text = paragraph.text
            for field, value in [("{{ lugar }}", lugar), ("{{ fecha }}", fecha), ("{{ senores }}", senores)]:
                text = text.replace(field, value)
            for field, value in actividades.items():
                text = text.replace(f"{{ {field} }}", value)
            for field, value in resultados.items():
                text = text.replace(f"{{ {field} }}", value)
            for field, value in observaciones.items():
                text = text.replace(f"{{ {field} }}", value)
            paragraph.text = text

        # Guardar archivo Word con un nuevo nombre
        edited_filename = filedialog.asksaveasfilename(
            defaultextension=".docx", filetypes=[("Archivo Word", "*.docx")])
        if edited_filename:
            document.save(edited_filename)
            print("Archivo guardado correctamente.")
        else:
            print("No se guardó el archivo.")

    except Exception as e:
        print(e)

if __name__ == "__main__":
    main()

I get Package not found at and I change the file and it doesn't work either.

try changing the file path but the idea of the code is to open the file explorer and look for the document, not to supply a specific path.

InSync
  • 4,851
  • 4
  • 8
  • 30
  • 2
    _I get Package not found at_ That is not a standard Python error. Where, exactly, is it coming from? It would help if you actually posted the exact complete error message. "Package not found at" is, at best, incomplete... – John Gordon Jun 24 '23 at 01:39

0 Answers0