-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgerar_excel.py
More file actions
102 lines (75 loc) · 2.37 KB
/
Copy pathgerar_excel.py
File metadata and controls
102 lines (75 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import json
from openpyxl import Workbook
from tkinter import Tk, filedialog, messagebox
import os
# ==============================
# CONFIGURAÇÃO (EDITÁVEL)
# ==============================
MAP = {
"nome": "username",
"usuario": "user",
"admin": "admin",
"tecnico": "technical"
}
# ==============================
# FUNÇÕES
# ==============================
def selecionar_arquivo():
Tk().withdraw()
arquivo = filedialog.askopenfilename(
title="Selecione o arquivo JSON",
filetypes=[("Arquivos JSON", "*.json")]
)
return arquivo
def extrair_lista(data):
if isinstance(data, list):
return data
elif isinstance(data, dict) and "content" in data:
return data["content"]
else:
raise Exception("Formato de JSON não reconhecido")
def processar_json(caminho):
with open(caminho, "r", encoding="utf-8") as f:
data = json.load(f)
return extrair_lista(data)
def gerar_excel(lista, caminho_saida):
wb = Workbook()
ws = wb.active
ws.title = "Usuarios"
# Cabeçalho
ws.append(["Nome", "Usuario", "Tipo Acesso", "Admin", "Tecnico"])
for user in lista:
nome = user.get(MAP["nome"], "")
usuario = "@" + str(user.get(MAP["usuario"], ""))
admin_bool = user.get(MAP["admin"], False)
tecnico_bool = user.get(MAP["tecnico"], False)
admin = "Sim" if admin_bool else "Não"
tecnico = "Sim" if tecnico_bool else "Não"
if admin_bool and tecnico_bool:
tipo = "Administrador / Técnico"
elif admin_bool:
tipo = "Administrador"
elif tecnico_bool:
tipo = "Técnico"
else:
tipo = ""
ws.append([nome, usuario, tipo, admin, tecnico])
wb.save(caminho_saida)
# ==============================
# EXECUÇÃO
# ==============================
def main():
try:
arquivo = selecionar_arquivo()
if not arquivo:
messagebox.showwarning("Aviso", "Nenhum arquivo selecionado.")
return
lista = processar_json(arquivo)
pasta = os.path.dirname(arquivo)
saida = os.path.join(pasta, "usuarios.xlsx")
gerar_excel(lista, saida)
messagebox.showinfo("Sucesso", f"Planilha gerada em:\n{saida}")
except Exception as e:
messagebox.showerror("Erro", str(e))
if __name__ == "__main__":
main()