在本文中,我们将分析一个使用 wxPython 和 Pandas 库编写的 Python 应用程序,名为 “XLSX Analyzer and Web Opener”。该应用程序的核心功能是:从 Excel 文件中读取数据并显示在网格中,此外,还允许用户使用 Google Chrome 批量打开 Excel 文件中的 URL 列表。
C:\pythoncode\new\analysisxlsx.py
全部代码
import wx
import wx.grid
import pandas as pd
import subprocess
import os
CHROME_PATH = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
class XlsxAnalyzerFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title='XLSX Analyzer and Web Opener', size=(1200, 800))
panel = wx.Panel(self)
main_sizer = wx.BoxSizer(wx.VERTICAL)
self.file_picker = wx.FilePickerCtrl(panel, wildcard="Excel files (*.xlsx)|*.xlsx")
self.file_picker.Bind(wx.EVT_FILEPICKER_CHANGED, self.on_file_selected)
main_sizer.Add(self.file_picker, 0, wx.ALL | wx.EXPAND, 10)
self.grid = wx.grid.Grid(panel)
main_sizer.Add(self.grid, 1, wx.ALL | wx.EXPAND, 10)
open_button = wx.Button(panel, label='Open URLs in Chrome')
open_button.Bind(wx.EVT_BUTTON, self.on_open_urls)
main_sizer.Add(open_button, 0, wx.ALL | wx.CENTER, 10)
panel.SetSizer(main_sizer)
self.Layout()
self.Show()
self.grid_created = False
def on_file_selected(self, event):
file_path = self.file_picker.GetPath()
if file_path:
try:
df = pd.read_excel(file_path, sheet_name='sheet1')
expected_columns = [
"blog-list-box href", "course-img src", "blog-list-box-top",
"blog-list-content", "article-type", "view-time-box", "view-num",
"give-like-num", "comment-num", "comment-num 2", "btn-edit-article href"
]
if not all(col in df.columns for col in expected_columns):
raise ValueError("Excel file does not contain all expected columns")
self.update_grid(df)
except Exception as e:
wx.MessageBox(f'Error reading file: {
str(e)}', 'Error', wx.OK | wx.ICON_ERROR)
def update_grid(self, df