优化上一篇的gui界面生成

复制代码
import pandas as pd
import json
import datetime
import os
import tkinter as tk
from tkinter import filedialog


def convert_to_excel():
    now = datetime.datetime.now()
    formatted_time = now.strftime("%Y-%m-%d_%H-%M-%S")

    # Open file dialog for selecting a file
    file_path = filedialog.askopenfilename(filetypes=[('Text files', '*.txt')])

    # Check if a file was selected
    if file_path:
        # Read JSON data from the selected file with permissive error handling
        with open(file_path, 'r', encoding='utf-8', errors='replace') as file:
            json_data = json.load(file)

        # Extract the 'data' part from JSON
        data_list = json_data.get("data", [])

        # Convert to DataFrame
        df = pd.DataFrame(data_list)

        # Save the DataFrame to an Excel file
        output_file_path = f"{formatted_time}.xlsx"
        df.to_excel(output_file_path, index=False)

        # Display a success message
        result_label.config(text=f"Conversion successful. File saved as {output_file_path}")


# Create the main application window
app = tk.Tk()
app.title("JSON to Excel Converter")

# Calculate the center coordinates for the window
screen_width = app.winfo_screenwidth()
screen_height = app.winfo_screenheight()
window_width = 400  # Adjust the width as needed
window_height = 150  # Adjust the height as needed

x_position = (screen_width - window_width) // 2
y_position = (screen_height - window_height) // 2

# Set the window geometry
app.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")

# Create a button for converting to Excel
convert_button = tk.Button(app, text="Convert to Excel", command=convert_to_excel)
convert_button.pack(pady=20)

# Create a label for displaying the result message
result_label = tk.Label(app, text="")
result_label.pack()

# Run the application loop
app.mainloop()
相关推荐
武天旭6 小时前
AI安全--AI大模型的毒性偏见与安全对齐实践
人工智能·python·深度学习
视觉小萌新7 小时前
AI Class 3 · Chapter 2-3:函数、异常、文件读写+JSON / JSONL 读写(小白版)
人工智能·python·json
小小张说故事8 小时前
Python 的 obj.x 到底按什么顺序找?拆开数据描述符、实例字典和 __getattr__ 那条优先级链
python
用户11481867894849 小时前
水印的实现方式逻辑
前端·python
贾公子9 小时前
从单条内容到账号维度——AIGC 风险评分的工程实现
后端·python
木头左11 小时前
【Python】不点浏览器发博客:逆向 CSDN 创作中心接口做 md-publish 技能
python·自动化·api
Tt有了t11 小时前
设计模式速查
java·python·设计模式
deepseek2312 小时前
Python日志拖慢请求?QueueHandler排队、丢失与退出实测
python·性能优化·logging·后端开发·queuehandler
用户912940468484212 小时前
Codex 从零到能交付(05):第一次改代码:生成一个文件清单脚本
python
Dmz丶12 小时前
前端转 AI 100 天 Day 38:Token 管理与成本优化——别让 API 账单爆炸
python