feat: implement tkinter file dialog for CSV selection with terminal fallback
This commit is contained in:
@@ -173,14 +173,39 @@ class CSVReaderService:
|
|||||||
# INTERACTION & CONFIRMATION HELPER (CLI)
|
# INTERACTION & CONFIRMATION HELPER (CLI)
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
def get_csv_filepath() -> str:
|
def get_csv_filepath() -> str:
|
||||||
"""Solicita o caminho do arquivo CSV ao usuário via terminal."""
|
"""Abre uma janela nativa do Windows (filedialog) para seleção do arquivo CSV.
|
||||||
|
Caso seja cancelado ou indisponível, cai para a seleção via terminal."""
|
||||||
default_csv = "Extrato_Bradesco_Julho_2026_v3.xlsx - despesas.csv"
|
default_csv = "Extrato_Bradesco_Julho_2026_v3.xlsx - despesas.csv"
|
||||||
print("\n" + "=" * 75)
|
print("\n" + "=" * 75)
|
||||||
print(" 📊 AUTOMAÇÃO DE DESPESAS SIGA - SELEÇÃO DE ARQUIVO CSV")
|
print(" 📊 AUTOMAÇÃO DE DESPESAS SIGA - SELEÇÃO DE ARQUIVO CSV")
|
||||||
print("=" * 75)
|
print("=" * 75)
|
||||||
|
|
||||||
|
# 1. Tenta abrir a janela nativa de escolha de arquivos do Windows
|
||||||
|
try:
|
||||||
|
import tkinter as tk
|
||||||
|
from tkinter import filedialog
|
||||||
|
|
||||||
|
logger.info("🗔 Abrindo janela do Windows para seleção do arquivo CSV...")
|
||||||
|
root = tk.Tk()
|
||||||
|
root.withdraw()
|
||||||
|
root.attributes("-topmost", True)
|
||||||
|
|
||||||
|
selected_file = filedialog.askopenfilename(
|
||||||
|
title="Selecione o arquivo CSV de Despesas para o SIGA",
|
||||||
|
filetypes=[("Arquivos CSV (*.csv)", "*.csv"), ("Todos os arquivos (*.*)", "*.*")],
|
||||||
|
initialdir=os.getcwd(),
|
||||||
|
)
|
||||||
|
root.destroy()
|
||||||
|
|
||||||
|
if selected_file and os.path.exists(selected_file):
|
||||||
|
print(f" Arquivo selecionado na janela: '{selected_file}'")
|
||||||
|
return selected_file
|
||||||
|
except Exception as gui_err:
|
||||||
|
logger.debug(f"Interface gráfica tkinter não iniciada: {gui_err}")
|
||||||
|
|
||||||
|
# 2. Fallback para prompt de terminal
|
||||||
if os.path.exists(default_csv):
|
if os.path.exists(default_csv):
|
||||||
print(f" Arquivo padrão detectado: '{default_csv}'")
|
print(f" Arquivo padrão detectado no diretório: '{default_csv}'")
|
||||||
|
|
||||||
user_input = input(f"\nDigite o caminho do arquivo .csv [Pressione ENTER para usar '{default_csv}']: ").strip().strip('"').strip("'")
|
user_input = input(f"\nDigite o caminho do arquivo .csv [Pressione ENTER para usar '{default_csv}']: ").strip().strip('"').strip("'")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user