PyQt5 郵件發送小軟體,實例。

PyQt5 郵件發送小軟體,實例。

來自專欄 Python實現圖形界面-PyQt5

5個月前因為業務需要用PyQt5寫了一個發送郵件的小軟體,很多地方不成熟,分享出來希望對感興趣的朋友有所幫助,歡迎看到疏漏的地方多多指正。源碼可在github上下載。

github地址:wangweifeng2018/MailSend_Sotfware

----------------------------------------------------------------------------------------------------------------------------

該軟體主要有兩個頁面,發送郵件界面,查看歷史界面。實現固定格式文件上傳,為郵件添加pdf格式附件。批量發送到指定賬戶的功能。幫助文檔有簡單的使用指南。

  1. 先從第一個郵件發送界面開始:

單獨打開(打開方式:下載源碼,或者複製代碼到本地,然後雙擊; 或者在eric6裡面按F2)

界面是這樣的:

  • 界面布局代碼(只能實現界面,現在還沒有功能實現,實現功能的函數寫在Call_page1.py腳本里,因為要實現頁面邏輯分離。):

#!/usr/bin/env python#encoding=utf-8import pandas as pdimport sysfrom PyQt5.QtWidgets import (QApplication, QFormLayout, QTableView,QLineEdit, QLabel, QAbstractItemView, QPushButton, QVBoxLayout,QHBoxLayout, QWidget, QTextEdit, QGroupBox, QProgressBar) class TextEdit(QTextEdit): #重定義QTextEdit類 def __init__(self, parent=None): super(TextEdit, self).__init__(parent) self.setAcceptDrops(True) def dropEvent(self, event): fname = event.mimeData().text() filename = fname.split("///")[1] if filename.endswith(".csv"): try: df = pd.read_csv(filename, header = None, encoding="utf-8") except: df = pd.read_csv(filename, header = None, encoding="gbk") self.setPlainText(df.to_string()) self.dataframe = df #print("##############Page1.dataframe:", self.dataframe) #print(df) else: self.setPlainText("無法識別文件類型") class Page1Win(QWidget): def __init__(self, parent=None): super(Page1Win, self).__init__(parent) self.createBox1() self.createBox2() # Main Layout mainLayout = QVBoxLayout() mainLayout.addWidget(self.box1Group) mainLayout.addWidget(self.box2Group) self.setLayout(mainLayout) self.setWindowTitle("Page1-wf") def createBox1(self): #BOX1 self.box1Group = QGroupBox("樣品信息") #col1 self.fileButton1 = QPushButton("選擇文件夾") self.fileButton1.setStyleSheet("background-color:rgb(020,150,150);" "font:bold 10pt Comic Sans MS") self.fileButton1.setToolTip("打開所有檢測樣品PDF報告所在的文件夾") self.fileButton2 = QPushButton("打開文件") self.fileButton2.setStyleSheet("background-color:rgb(020,150,150);" "font:bold 10pt Comic Sans MS") self.fileButton2.setToolTip("點擊此處選擇包含客戶信息的文件,文件格式需要為CSV") self.fileButton3 = QPushButton("統計pdf文件") self.fileButton3.setStyleSheet("background-color:rgb(020,150,150);" "font:bold 10pt Comic Sans MS") self.fileButton3.setToolTip("點擊此處檢測統計客戶信息及pdf報告文件") col1layout= QVBoxLayout() col1layout.addWidget(self.fileButton1) col1layout.addWidget(self.fileButton2) col1layout.addStretch(1) col1layout.addWidget(self.fileButton3) col1layout.addStretch(8) #col2 self.pdfText = QLineEdit() self.pdfText.selectAll() self.pdfText.setFocus() self.samplePdTv = QTableView(self) col2layout = QVBoxLayout() col2layout.addWidget(self.pdfText) col2layout.addWidget(self.samplePdTv) box1Layout = QHBoxLayout() box1Layout.addLayout(col1layout) box1Layout.addLayout(col2layout) self.box1Group.setLayout(box1Layout) def createBox2(self): self.sendButton = QPushButton("發送") self.sendButton.setStyleSheet("background-color:rgb(020,150,150);" "font:bold 10pt Comic Sans MS") self.sendButton.setToolTip("批量發送郵件") self.progressBar = QProgressBar() self.progressBar.setFormat("%v") self.infoTv = QTableView(self) self.box2Group = QGroupBox("報告結果統計") self.box2Layout = QFormLayout() self.box2Layout.addRow(self.sendButton, self.progressBar) self.box2Layout.addRow(QLabel(發送失敗郵件), self.infoTv) self.box2Group.setLayout(self.box2Layout) if __name__ == __main__: app = QApplication(sys.argv) win = Page1Win() win.show() sys.exit(app.exec_())

  • Call_page1.py 郵件發送界面實現功能的代碼:

#!/usr/bin/env python#encoding=utf-8 import sysimport globimport pandas as pdfrom PandasModel import PandasModelfrom PyQt5 import QtWidgetsfrom PyQt5.QtWidgets import (QApplication,QFileDialog)from Page1 import Page1Winclass CallPage1(Page1Win): def __init__(self): super(CallPage1, self).__init__() #初始化文件上傳狀態為False self._Folder = False self._Info = False self._pdfChk = False self.pdfFolder = "" self.sampleDF = pd.DataFrame() #樣品上傳狀態 self.fileButton1.clicked.connect(self.getFolderName) self.fileButton2.clicked.connect(self.openInfoFile) #統計Pdf文件 self.fileButton3.clicked.connect(self.checkPdf) def openInfoFile(self): fname, _ = QFileDialog.getOpenFileName(self, 樣品信息文件, c:\) if fname: self._Info = True print("#####Tip: 樣品信息文件
"+fname) if fname.endswith(.csv): df = pd.read_csv(fname, header=None, skiprows=1, encoding=utf-8, names=[序號, 檢測項目, 客戶編碼, 客戶姓名, 郵箱]) elif fname.endswith(.xls) or fname.endswith(.xlsx) : df = pd.read_excel(fname, header=None, skiprows=1, encoding=utf-8, names=[序號, 檢測項目, 客戶編碼, 客戶姓名, 郵箱]) else: sys.exit(1) model = PandasModel(df) self.samplePdTv.setModel(model) self.sampleDF = df return def getFolderName(self): folderName= str(QFileDialog.getExistingDirectory(self, "選擇文件夾")) if folderName: self._Folder = True print("Tip: PDF報告文件夾:
"+folderName) self.pdfText.setText(folderName) self.pdfFolder = folderName return (folderName) def checkPdf(self): filepath = self.pdfFolder if not filepath: QtWidgets.QMessageBox.warning( self, 錯誤, 請選擇pdf報告所在文件夾) print("請先選擇包含PDF報告的文件夾!") if not self._Folder: QtWidgets.QMessageBox.warning( self, 錯誤, 請選擇pdf報告所在文件夾) print("請先選擇包含PDF報告的文件夾!") if not self._Info: QtWidgets.QMessageBox.warning( self, 錯誤, 請上傳樣品信息文件) print("請先上傳樣品信息文件!") else: self.sampleDF[報告位置] = self.sampleDF[客戶編碼].apply(lambda x: filepath+"/"+str(x)+".pdf") self.sampleDF[pdf報告] = self.sampleDF[客戶編碼].apply(lambda x: glob.glob(filepath+"/*"+str(x)+"*.pdf")) self.sampleDF = self.sampleDF.drop([報告位置], axis=1) self.samplePdTv.setModel(PandasModel(self.sampleDF)) self._pdfChk = True return if __name__ == __main__: app = QApplication(sys.argv) win = CallPage1() win.show() sys.exit(app.exec_())

會陸續更新更詳細的說明。。。


推薦閱讀:

TAG:PyQt5 | | Python | 圖形用戶界面 |