python提取圖片數據寫入excel,并打包為exe可執行文件
- 1. 以下面的圖片為例
- 2. python環境需要的依賴包
- 3. 創建交互式窗口
- 4. 讀取文件夾下的所有文件并提取數據
- 5. 提取圖片中字段的代碼
- 6. 打包代碼為exe可執行文件
- 安裝打包依賴文件
- 運行打包代碼
1. 以下面的圖片為例
2. python環境需要的依賴包
import os
import re
import pytesseract
from tkinter import Tk, filedialog
from PIL import Image, ImageOps
import pandas as pd
3. 創建交互式窗口
# 創建Tkinter窗口
root = Tk()
root.withdraw() # 隱藏主窗口# 彈出選擇文件夾對話框
folder_path = filedialog.askdirectory(title='選擇圖片文件夾')
4. 讀取文件夾下的所有文件并提取數據
# 彈出選擇文件夾對話框
folder_path = filedialog.askdirectory(title='選擇圖片文件夾')
# 配置Tesseract路徑,如果已配置環境變量則可以省略這步
pytesseract.pytesseract.tesseract_cmd = r'D:\toolsoft\tesseractocr\tesseract.exe'
# 創建一個字典來存儲數據
data = {'file name': [],'CBF<30% volume': [],'Tmax>6.0s volume': [],'Mismatch volume': [],'Mismatch ratio': []
}
# 如果用戶取消選擇,返回空路徑
if not folder_path:print('未選擇文件夾。')
else:print(f'選擇的文件夾路徑為:{folder_path}')# 遍歷文件夾中的所有文件for filename in os.listdir(folder_path):file_path = os.path.join(folder_path, filename)if os.path.isfile(file_path):# 檢查文件是否為圖片文件(可以根據實際需求擴展這個條件)if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):# 打開圖片try:image = Image.open(file_path)# 處理圖片,例如顯示、保存或進行其他操作data = from_fig_get_txt(data, image)data['file name'].append(f"{filename}")# 例如,顯示圖片# image.show()# 或者進行其他處理,如圖像處理、識別等# 這里可以添加你的其他代碼邏輯except OSError:print(f'無法打開文件:{file_path}')# 使用pandas將數據寫入Exceldf = pd.DataFrame(data)df.to_excel('output.xlsx', index=False)print('提取完成并已寫入output.xlsx文件。')
5. 提取圖片中字段的代碼
def from_fig_get_txt(data, image):# 將彩色圖像轉換為灰度圖像gray_image = ImageOps.grayscale(image)# 使用pytesseract提取圖片中的文字text = pytesseract.image_to_string(gray_image, lang='eng') # chi_sim使用簡體中文,'eng'用于英文# 將提取的文字按要求分割或處理lines = text.split('\n')for i, line in enumerate(lines):if 'CBF<30% volume' in line.strip(): # 跳過空行# 使用正則表達式進行匹配# 匹配CBF后面的數據cbf_match = re.search(r'CBF<(\d+%) volume:\s*(\S+)\s*ml', line)if cbf_match:# cbf_percent = cbf_match.group(1) # 提取CBF的百分比cbf_volume = cbf_match.group(2) # 提取CBF的體積# print(f"CBF百分比: {cbf_percent}, CBF體積: {cbf_volume} ml")data['CBF<30% volume'].append(f"{cbf_volume} ml")if 'Tmax>6.0s volume' in line.strip(): # 跳過空行# 匹配Tmax后面的數據tmax_match = re.search(r'Tmax>([\d.]+)s.*?volume:\s*([\d.]+)\s*ml', line)if tmax_match:# tmax_value = tmax_match.group(1) # 提取Tmax的數值tmax_volume = tmax_match.group(2) # 提取Tmax的體積# print(f"Tmax數值: {tmax_value} s, Tmax體積: {tmax_volume} ml")data['Tmax>6.0s volume'].append(f"{tmax_volume} ml")if 'Mismatch volume' in line.strip(): # 跳過空行# 匹配Mismatch后面的數據Mis_match = re.search(r':\s*(.*)$', line)Mis_volume = Mis_match.group(1) # 提取Tmax的體積# print(f"Tmax數值: {tmax_value} s, Tmax體積: {tmax_volume} ml")data['Mismatch volume'].append(f"{Mis_volume}")if 'Mismatch ratio' in line.strip(): # 跳過空行# 匹配Mismatch后面的數據Mis_ratio = re.search(r':\s*(.*)$', line)Mis_ratio = Mis_ratio.group(1) # 提取Tmax的體積# print(f"Tmax數值: {tmax_value} s, Tmax體積: {tmax_volume} ml")data['Mismatch ratio'].append(f"{Mis_ratio}")return data
6. 打包代碼為exe可執行文件
安裝打包依賴文件
pip install pyinstaller
運行打包代碼
pyinstaller --onefile yourpyfile.py
生成的 EXE 文件將在 dist 文件夾中