python提取特定格式的數(shù)據(jù)的操作方法
本文檔總結(jié)了 ExcelGridConverter.py
腳本所涉及的關(guān)鍵 Python 知識(shí)點(diǎn)。該腳本用于從多個(gè) Excel 文件中提取特定格式的數(shù)據(jù)并轉(zhuǎn)換為一個(gè)新的 Excel 文件。
導(dǎo)入庫(kù)
腳本使用了以下主要庫(kù):
tkinter
:用于創(chuàng)建圖形用戶界面。pandas
:用于處理 Excel 數(shù)據(jù)。os
:用于處理文件和目錄路徑。
import tkinter as tk from tkinter import filedialog, messagebox import pandas as pd import os
Pandas 數(shù)據(jù)處理
讀取 Excel 文件
使用 pd.read_excel
方法讀取 Excel 文件,并使用 sheet_name=None
參數(shù)讀取所有工作表。添加 index_col=None
參數(shù)以確保第一列不會(huì)被自動(dòng)設(shè)置為索引列。
source_df = pd.read_excel(file_path, sheet_name=None, index_col=None) source_data = source_df['一格一案']
數(shù)據(jù)提取
通過(guò) Pandas 的 iloc
方法,根據(jù)行列索引提取特定數(shù)據(jù)。
result_data = { '網(wǎng)格編號(hào)': source_data.iloc[1, 1], '責(zé)任段': source_data.iloc[1, 3], ... }
處理合并單元格數(shù)據(jù):
risk_check_path = "\n".join(source_data.iloc[9:19, 1].dropna().astype(str)) result_data['五、風(fēng)險(xiǎn)項(xiàng)點(diǎn)檢查路徑'] = risk_check_path
創(chuàng)建 DataFrame 并導(dǎo)出為 Excel 文件
將所有提取的數(shù)據(jù)放入一個(gè) DataFrame 中,并使用 to_excel
方法導(dǎo)出為 Excel 文件。
result_df = pd.DataFrame(all_data) result_df.to_excel(output_file_path, index=False)
Tkinter GUI 界面
創(chuàng)建主窗口
使用 tk.Tk
創(chuàng)建主窗口,并設(shè)置窗口標(biāo)題、大小和位置。
root = tk.Tk() root.title("Excel 轉(zhuǎn)換工具") root.geometry(f'{window_width}x{window_height}+{position_right}+{position_top}')
創(chuàng)建按鈕和標(biāo)簽
使用 tk.Button
和 tk.Label
創(chuàng)建按鈕和標(biāo)簽,并設(shè)置其屬性和布局。
title_label = tk.Label(root, text="Excel 轉(zhuǎn)換工具", 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)
文件操作
文件對(duì)話框
使用 filedialog.askopenfilenames
打開文件選擇對(duì)話框,允許用戶選擇多個(gè) Excel 文件。使用 filedialog.asksaveasfilename
打開文件保存對(duì)話框,允許用戶選擇保存路徑。
file_paths = filedialog.askopenfilenames(filetypes=[("Excel 文件", "*.xlsx")]) output_file_path = filedialog.asksaveasfilename(defaultextension=".xlsx", filetypes=[("Excel 文件", "*.xlsx")])
主要函數(shù)解釋
transform_to_result_format_specific
該函數(shù)從源數(shù)據(jù)中提取特定字段,并返回一個(gè)字典格式的結(jié)果數(shù)據(jù)。
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
該函數(shù)處理文件選擇、數(shù)據(jù)轉(zhuǎn)換和結(jié)果保存的主要邏輯。
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("成功", "文件已成功轉(zhuǎn)換并保存。")
總結(jié)
通過(guò)本腳本,我們學(xué)習(xí)了如何使用 Pandas 讀取和處理 Excel 數(shù)據(jù),如何使用 Tkinter 創(chuàng)建圖形用戶界面,以及如何處理文件對(duì)話框和文件操作。這些知識(shí)點(diǎn)在日常的 Python 開發(fā)中非常實(shí)用,特別是涉及數(shù)據(jù)處理和用戶界面的項(xiàng)目中。
到此這篇關(guān)于python提取特定格式的數(shù)據(jù)的文章就介紹到這了,更多相關(guān)python提取數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
教你使用TensorFlow2識(shí)別驗(yàn)證碼
驗(yàn)證碼是根據(jù)隨機(jī)字符生成一幅圖片,然后在圖片中加入干擾象素,本文主要介紹了 TensorFlow2識(shí)別驗(yàn)證碼,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06pygame+opencv實(shí)現(xiàn)讀取視頻幀的方法示例
由于pygame.movie.Movie.play()只支持MPEG格式的視頻,所以決定使用與opencv讀取視頻幀的畫面,本文就詳細(xì)的介紹了pygame+opencv實(shí)現(xiàn)讀取視頻幀,感興趣的可以了解一下2021-12-12Python實(shí)現(xiàn)定制自動(dòng)化業(yè)務(wù)流量報(bào)表周報(bào)功能【XlsxWriter模塊】
這篇文章主要介紹了Python實(shí)現(xiàn)定制自動(dòng)化業(yè)務(wù)流量報(bào)表周報(bào)功能,結(jié)合實(shí)例形式分析了Python基于XlsxWriter模塊操作xlsx文件生成報(bào)表圖的相關(guān)操作技巧,需要的朋友可以參考下2019-03-03Python批量生成特定尺寸圖片及圖畫任意文字的實(shí)例
今天小編就為大家分享一篇Python批量生成特定尺寸圖片及圖畫任意文字的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01