python提取特定格式的数据


Excel Grid Data Converter 知识点总结

本文档总结了 ExcelGridConverter.py 脚本所涉及的关键 Python 知识点。该脚本用于从多个 Excel 文件中提取特定格式的数据并转换为一个新的 Excel 文件。

目录

  1. 导入库
  2. [Pandas 数据处理](#Pandas 数据处理)
  3. [Tkinter GUI 界面](#Tkinter GUI 界面)
  4. 文件操作
  5. 主要函数解释
  6. 总结

导入库

脚本使用了以下主要库:

  • tkinter:用于创建图形用户界面。
  • pandas:用于处理 Excel 数据。
  • os:用于处理文件和目录路径。
python 复制代码
import tkinter as tk
from tkinter import filedialog, messagebox
import pandas as pd
import os

Pandas 数据处理

读取 Excel 文件

使用 pd.read_excel 方法读取 Excel 文件,并使用 sheet_name=None 参数读取所有工作表。添加 index_col=None 参数以确保第一列不会被自动设置为索引列。

python 复制代码
source_df = pd.read_excel(file_path, sheet_name=None, index_col=None)
source_data = source_df['一格一案']

数据提取

通过 Pandas 的 iloc 方法,根据行列索引提取特定数据。

python 复制代码
result_data = {
    '网格编号': source_data.iloc[1, 1],
    '责任段': source_data.iloc[1, 3],
    ...
}

处理合并单元格数据:

python 复制代码
risk_check_path = "\n".join(source_data.iloc[9:19, 1].dropna().astype(str))
result_data['五、风险项点检查路径'] = risk_check_path

创建 DataFrame 并导出为 Excel 文件

将所有提取的数据放入一个 DataFrame 中,并使用 to_excel 方法导出为 Excel 文件。

python 复制代码
result_df = pd.DataFrame(all_data)
result_df.to_excel(output_file_path, index=False)

Tkinter GUI 界面

创建主窗口

使用 tk.Tk 创建主窗口,并设置窗口标题、大小和位置。

python 复制代码
root = tk.Tk()
root.title("Excel 转换工具")
root.geometry(f'{window_width}x{window_height}+{position_right}+{position_top}')

创建按钮和标签

使用 tk.Buttontk.Label 创建按钮和标签,并设置其属性和布局。

python 复制代码
title_label = tk.Label(root, text="Excel 转换工具", font=("Arial", 18))
title_label.pack(pady=20)

select_button = tk.Button(root, text="选择 Excel 文件", command=select_files, font=("Arial", 12))
select_button.pack(pady=10)

文件操作

文件对话框

使用 filedialog.askopenfilenames 打开文件选择对话框,允许用户选择多个 Excel 文件。使用 filedialog.asksaveasfilename 打开文件保存对话框,允许用户选择保存路径。

python 复制代码
file_paths = filedialog.askopenfilenames(filetypes=[("Excel 文件", "*.xlsx")])
output_file_path = filedialog.asksaveasfilename(defaultextension=".xlsx", filetypes=[("Excel 文件", "*.xlsx")])

主要函数解释

transform_to_result_format_specific

该函数从源数据中提取特定字段,并返回一个字典格式的结果数据。

python 复制代码
def transform_to_result_format_specific(source_data, source_file_path):
    risk_check_path = "\n".join(source_data.iloc[9:19, 1].dropna().astype(str))
    result_data = { ... }
    return result_data

select_files

该函数处理文件选择、数据转换和结果保存的主要逻辑。

python 复制代码
def select_files():
    file_paths = filedialog.askopenfilenames(filetypes=[("Excel 文件", "*.xlsx")])
    all_data = []
    for file_path in file_paths:
        source_df = pd.read_excel(file_path, sheet_name=None, index_col=None)
        source_data = source_df['一格一案']
        transformed_data = transform_to_result_format_specific(source_data, file_path)
        all_data.append(transformed_data)
    result_df = pd.DataFrame(all_data)
    output_file_path = filedialog.asksaveasfilename(defaultextension=".xlsx", filetypes=[("Excel 文件", "*.xlsx")])
    if output_file_path:
        result_df.to_excel(output_file_path, index=False)
        messagebox.showinfo("成功", "文件已成功转换并保存。")

总结

通过本脚本,我们学习了如何使用 Pandas 读取和处理 Excel 数据,如何使用 Tkinter 创建图形用户界面,以及如何处理文件对话框和文件操作。这些知识点在日常的 Python 开发中非常实用,特别是涉及数据处理和用户界面的项目中。


相关推荐
于越海1 小时前
材料电子理论核心四个基本模型的python编程学习
开发语言·笔记·python·学习·学习方法
中年程序员一枚1 小时前
Springboot报错Template not found For name “java/lang/Object_toString.sql
java·spring boot·python
AI Echoes2 小时前
LangChain 非分割类型的文档转换器使用技巧
人工智能·python·langchain·prompt·agent
程序之巅3 小时前
VS code 远程python代码debug
android·java·python
__如风__3 小时前
onlyoffice文档转换服务离线部署
python
今晚务必早点睡3 小时前
写一个Python接口:发送支付成功短信
开发语言·python
ada7_3 小时前
LeetCode(python)22.括号生成
开发语言·数据结构·python·算法·leetcode·职场和发展
2501_941871453 小时前
面向微服务链路追踪与全局上下文管理的互联网系统可观测性设计与多语言工程实践分享
大数据·数据库·python
luoluoal3 小时前
基于python的语音和背景音乐分离算法及系统(源码+文档)
python·mysql·django·毕业设计·源码
love530love4 小时前
EPGF 新手教程 12在 PyCharm(中文版 GUI)中创建 Poetry 项目环境,并把 Poetry 做成“项目自包含”(工具本地化为必做环节)
开发语言·ide·人工智能·windows·python·pycharm·epgf