Skip to content

Commit

Permalink
支持导出csv格式聊天记录
Browse files Browse the repository at this point in the history
  • Loading branch information
LC044 committed Oct 26, 2023
1 parent f1d0ff2 commit 0c00298
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 45 deletions.
66 changes: 56 additions & 10 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/DataBase/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def get_all_message(wxid):
sql = '''
select * from message
where talker = ?
order by msgId
order by createTime
'''
cursor.execute(sql, [wxid])
return cursor.fetchall()
Expand Down
110 changes: 87 additions & 23 deletions app/DataBase/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import time

import docx
import numpy as np
import pandas as pd
import xmltodict
from PyQt5.QtCore import *
from docx import shared
Expand All @@ -13,6 +15,9 @@
from . import data


# import data


def IS_5_min(last_m, now_m):
"""
#! 判断两次聊天时间是不是大于五分钟
Expand Down Expand Up @@ -43,15 +48,20 @@ class Output(QThread):
rangeSignal = pyqtSignal(int)
okSignal = pyqtSignal(int)
i = 1
CSV = 0
DOCX = 1
HTML = 2

def __init__(self, Me, ta_u, parent=None):
def __init__(self, Me, ta_u, parent=None, type_=DOCX):
super().__init__(parent)
self.Me = Me
self.sec = 2 # 默认1000秒
self.ta_username = ta_u
self.my_avatar = self.Me.my_avatar
self.ta_avatar = data.get_avator(ta_u)
self.msg_id = 0
self.output_type = type_
self.total_num = 0

def merge_docx(self, conRemark, n):
origin_docx_path = f"{os.path.abspath('.')}/data/聊天记录/{conRemark}"
Expand Down Expand Up @@ -87,26 +97,46 @@ def progress(self, value):
self.okSignal.emit(1)
self.progressSignal.emit(self.i)

def to_csv(self, conRemark, path):
origin_docx_path = f"{os.path.abspath('.')}/data/聊天记录/{conRemark}"
messages = data.get_all_message(self.ta_username)
# print(messages)
self.Child0 = ChildThread(self.Me, self.ta_username, messages, conRemark, 0, type_=ChildThread.CSV)
self.Child0.progressSignal.connect(self.progress)
self.Child0.start()
print("成功导出CSV文件:", origin_docx_path)

def run(self):
self.Child = {}
if 1:
conRemark = data.get_conRemark(self.ta_username)
data.mkdir(f"{os.path.abspath('.')}/data/聊天记录/{conRemark}")
conRemark = data.get_conRemark(self.ta_username)
data.mkdir(f"{os.path.abspath('.')}/data/聊天记录/{conRemark}")
if self.output_type == self.DOCX:
self.Child = {}
if 1:
messages = data.get_all_message(self.ta_username)
self.total_num = len(messages)
self.rangeSignal.emit(self.total_num)
l = len(messages)
self.n = 10
for i in range(self.n):
q = i * (l // self.n)
p = (i + 1) * (l // self.n)
if i == self.n - 1:
p = l
len_data = messages[q:p]
# self.to_docx(len_data, i, conRemark)
self.Child[i] = ChildThread(self.Me, self.ta_username, len_data, conRemark, i)
self.Child[i].progressSignal.connect(self.progress)
self.Child[i].start()
elif self.output_type == self.CSV:
# print("线程导出csv")
# self.to_csv(self.ta_username, "path")
origin_docx_path = f"{os.path.abspath('.')}/data/聊天记录/{self.ta_username}"
messages = data.get_all_message(self.ta_username)
self.total_num = len(messages)
self.rangeSignal.emit(self.total_num)
l = len(messages)
self.n = 10
for i in range(self.n):
q = i * (l // self.n)
p = (i + 1) * (l // self.n)
if i == self.n - 1:
p = l
len_data = messages[q:p]
# self.to_docx(len_data, i, conRemark)
self.Child[i] = ChildThread(self.Me, self.ta_username, len_data, conRemark, i)
self.Child[i].progressSignal.connect(self.progress)
self.Child[i].start()
# print(messages)
self.Child0 = ChildThread(self.Me, self.ta_username, messages, conRemark, 0, type_=ChildThread.CSV)
self.Child0.progressSignal.connect(self.progress)
self.Child0.run()
self.okSignal.emit(1)


class ChildThread(QThread):
Expand All @@ -116,8 +146,11 @@ class ChildThread(QThread):
progressSignal = pyqtSignal(int)
rangeSignal = pyqtSignal(int)
i = 1
CSV = 0
DOCX = 1
HTML = 2

def __init__(self, Me, ta_u, message, conRemark, num, parent=None):
def __init__(self, Me, ta_u, message, conRemark, num, parent=None, type_=DOCX):
super().__init__(parent)
self.Me = Me
self.sec = 2 # 默认1000秒
Expand All @@ -128,6 +161,7 @@ def __init__(self, Me, ta_u, message, conRemark, num, parent=None):
self.conRemark = conRemark
self.message = message
self.msg_id = 0
self.output_type = type_

def create_table(self, doc, isSend):
'''
Expand Down Expand Up @@ -374,11 +408,41 @@ def to_docx(self, messages, i, conRemark):
print(filename)
doc.save(filename)

def to_csv(self, messages, i, conRemark):
'''创建联系人目录'''
# print('123', messages)
filename = f"{os.path.abspath('.')}/data/聊天记录/{conRemark}/{conRemark}.csv"
last_timestamp = 1601968667000
columns = ["用户名", "消息内容", "发送时间", "发送状态", "消息类型", "isSend", "msgId"]
df = pd.DataFrame()
df["用户名"] = np.array(list(map(lambda x: x[7], messages)))
df["消息内容"] = np.array(list(map(lambda x: x[8], messages)))
df["发送时间"] = np.array(list(map(lambda x: time_format(x[6]), messages)))
df["发送状态"] = np.array(list(map(lambda x: x[3], messages)))
df["消息类型"] = np.array(list(map(lambda x: x[2], messages)))
df["isSend"] = np.array(list(map(lambda x: x[4], messages)))
df["msgId"] = np.array(list(map(lambda x: x[0], messages)))
df.to_csv(filename)
# df.to_csv('data.csv')
print(df)
self.progressSignal.emit(self.num)

def to_html(self, messages, i, conRemark):
pass

def run(self):
self.to_docx(self.message, self.num, self.conRemark)
if self.output_type == self.DOCX:
# print("导出docx")
self.to_docx(self.message, self.num, self.conRemark)
elif self.output_type == self.CSV:
print("导出csv001")
# print('00', self.message[0])
self.to_csv(self.message, self.num, self.conRemark)


if __name__ == '__main__':
me = data.Me('wxid_27hqbq7vx5hf22')
t = Output(Me=me, ta_u='wxid_q3ozn70pweud22')
# wxid_0o18ef858vnu22
# wxid_fdkbu92el15h22
me = data.Me('wxid_fdkbu92el15h22')
t = Output(Me=me, ta_u='wxid_0o18ef858vnu22', type_=Output.CSV)
t.run()
41 changes: 35 additions & 6 deletions app/Ui/contact/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, Me, parent=None):
def initui(self):
# 槽函数连接
self.btn_back.clicked.connect(self.back)
self.btn_output.clicked.connect(self.output)
# self.btn_output.clicked.connect(self.output)
self.btn_analysis.clicked.connect(self.analysis)
self.btn_emotion.clicked.connect(self.emotionale_Analysis)
self.btn_report.clicked.connect(self.annual_report)
Expand All @@ -86,6 +86,19 @@ def initui(self):
self.userinfo.progressBar.setVisible(False)
self.stackedWidget.addWidget(self.frame)
# self.lay0.addWidget(self.frame)
menu = QMenu(self)
self.toDocxAct = QAction(QIcon('app/data/icons/word.svg'), '导出Docx', self)
self.toCSVAct = QAction(QIcon('app/data/icons/csv.svg'), '导出CSV', self)
self.toHtmlAct = QAction(QIcon('app/data/icons/html.svg'), '导出HTML', self)
self.toolButton_output.setPopupMode(QToolButton.MenuButtonPopup)
menu.addAction(self.toDocxAct)
menu.addAction(self.toCSVAct)
menu.addAction(self.toHtmlAct)
self.toolButton_output.setMenu(menu)
# self.toolButton_output.addSeparator()
self.toHtmlAct.triggered.connect(self.output)
self.toDocxAct.triggered.connect(self.output)
self.toCSVAct.triggered.connect(self.output)

def showContact(self):
"""
Expand Down Expand Up @@ -160,11 +173,27 @@ def output(self):
:return:
"""
self.setViewVisible(self.now_talkerId)
self.outputThread = output.Output(self.Me, self.now_talkerId)
self.outputThread.progressSignal.connect(self.output_progress)
self.outputThread.rangeSignal.connect(self.set_progressBar_range)
self.outputThread.okSignal.connect(self.hide_progress_bar)
self.outputThread.start()

if self.sender() == self.toDocxAct:
self.outputThread = output.Output(self.Me, self.now_talkerId)
self.outputThread.progressSignal.connect(self.output_progress)
self.outputThread.rangeSignal.connect(self.set_progressBar_range)
self.outputThread.okSignal.connect(self.hide_progress_bar)
self.outputThread.start()
elif self.sender() == self.toCSVAct:
print('开始导出csv')
self.outputThread = output.Output(self.Me, self.now_talkerId, type_=output.Output.CSV)
self.outputThread.progressSignal.connect(self.output_progress)
self.outputThread.rangeSignal.connect(self.set_progressBar_range)
self.outputThread.okSignal.connect(self.hide_progress_bar)
self.outputThread.start()
print('导出csv')
elif self.sender() == self.toHtmlAct:
print('功能暂未实现')
QMessageBox.warning(self,
"别急别急",
"马上就实现该功能"
)

def hide_progress_bar(self, int):
reply = QMessageBox(self)
Expand Down
Loading

0 comments on commit 0c00298

Please sign in to comment.