使用pyqt5读取outlook.msg文件

2024-09-28 16:57:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个应用程序,加载outlook.msg文件并将邮件正文中的数据保存在word文件中。现在我的问题有两个部分:1。在我的代码中哪里可以提取.msg文件主体中的数据。2如何创建另一个浏览按钮,将.msg文件正文中的数据保存在另一个位置的word文件中。你知道吗

import os
import sys
from PyQt5.QtGui import QPixmap,QStandardItemModel, QStandardItem
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QMainWindow, QLabel, QLineEdit, QCheckBox, 
QComboBox, QMessageBox, QRadioButton, QButtonGroup, QTextEdit, 
QProgressBar
from PyQt5.QtWidgets import QPushButton, QFileDialog, 
QTableWidget,QTableWidgetItem, QWidget, QTableView, QAbstractItemView, 
QToolTip, QApplication
from PyQt5.QtCore import QSize, QModelIndex, QItemSelection, 
QItemSelectionModel, QBasicTimer, QThread, pyqtSignal, Qt


import win32com.client
import pandas as pd
import pdb


class OutlookThread(QThread):
   signal = pyqtSignal('PyQt_PyObject')

   def __init__(self, path_to_outlook):
      QThread.__init__(self)
      self.path_to_outlook = path_to_outlook

   def run(self):
      self.outlookpath 
     =win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
      self.signal.emit(self.outlookpath)




class SetPathWindow(QMainWindow):
   def __init__(self):
       QMainWindow.__init__(self)
       self.setMinimumSize(QSize(700, 300))    
       self.setWindowTitle("Data Extraction From Email")

       self.label = QLabel(self)


       self.step1label = QLabel(self)
       self.step1label.setText("Load the .msg file")
       self.step1label.setStyleSheet('color: grey;font-weight: bold')
       self.step1label.adjustSize()
       self.step1label.move(50, 30)


       self.line1 = self.create_fields_empty(50, 70, 480, 30)
       self.line1.setPlaceholderText("Add the path of msg file...")

       button1 = self.push_button('Browse', 550, 70, 100, 32)
       button1.clicked.connect(self.get_outlook_file_path)

       button5 = self.push_button('Next >>', 300, 150, 100, 32)
       button5.clicked.connect(self.clicked) 

  def resource_path(self, relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

  def OutlookOpened(self, outlook_path):
    QApplication.restoreOverrideCursor()
    self.close()


  def create_fields_text(self, x, y, text):
    self.nameLabel = QLabel(self)
    self.nameLabel.setText(text)
    self.nameLabel.adjustSize()
    self.nameLabel.move(x, y)

def create_fields_empty(self, x, y, len, width):
    self.line = QLineEdit(self)
    self.line.move(x, y)
    self.line.resize(len, width)
    return self.line

def push_button(self, name, x, y, len, width):
    pybutton = QPushButton(name, self)
    pybutton.move(x, y)
    pybutton.resize(len, width)
    pybutton.setStyleSheet(
    "QPushButton { background-color: grey;color: white;font-weight: bold; border-radius: 5px}"
    "QPushButton:pressed { background-color: grey ;color: black;font-weight: bold; border-radius: 5px}"
    )
    return pybutton

def get_outlook_file_path(self):
    self.file1 = QFileDialog.getOpenFileName(self, "*.msg")
    print('1',type(self.file1),self.file1)
    self.line1.setText(self.file1[0])
    print('2',type(self.file1[0]),self.file1[0])
    self.outlook_thread = OutlookThread(self.file1[0])
    print('3',type(self.outlook_thread),self.outlook_thread)
    pdb.set_trace()
    self.outlook_thread.signal.connect(self.OutlookOpened)

def clicked(self):
    if self.line1.text() == '':
        QMessageBox().warning(self, "Msg file path not found!", "Please select a .msg outlook file", QMessageBox.Ok)
    else:
        QApplication.setOverrideCursor(Qt.WaitCursor)
        self.outlook_thread.start()




if __name__ == "__main__":
 app = QtWidgets.QApplication(sys.argv)
 mainWin = SetPathWindow()
 mainWin.show()
 sys.exit(app.exec_())

我还没有添加将outlook邮件正文转换为word的功能。我只想知道读取文件后数据在哪里。您可以使用函数简单地打印消息体中的信息。你知道吗


Tags: 文件pathfromimportselfdefmsgthread