mirror of
https://github.com/LC044/WeChatMsg
synced 2025-02-23 03:22:17 +08:00
加快docx导出速度
对话记录特别多的时候,越往后word文件越大,添加一个记录所需时间也越长。 先将每200条记录保存,最后合并在一起,将整个过程大大加快。
This commit is contained in:
parent
c89ae94f64
commit
a01819a061
@ -8,6 +8,7 @@ from docx import shared
|
|||||||
from docx.enum.table import WD_ALIGN_VERTICAL
|
from docx.enum.table import WD_ALIGN_VERTICAL
|
||||||
from docx.enum.text import WD_COLOR_INDEX, WD_PARAGRAPH_ALIGNMENT
|
from docx.enum.text import WD_COLOR_INDEX, WD_PARAGRAPH_ALIGNMENT
|
||||||
from docx.oxml.ns import qn
|
from docx.oxml.ns import qn
|
||||||
|
from docxcompose.composer import Composer
|
||||||
|
|
||||||
from app.DataBase import msg_db, hard_link_db
|
from app.DataBase import msg_db, hard_link_db
|
||||||
from app.DataBase.output import ExporterBase, escape_js_and_html
|
from app.DataBase.output import ExporterBase, escape_js_and_html
|
||||||
@ -284,10 +285,6 @@ class DocxExporter(ExporterBase):
|
|||||||
def export(self):
|
def export(self):
|
||||||
print(f"【开始导出 DOCX {self.contact.remark}】")
|
print(f"【开始导出 DOCX {self.contact.remark}】")
|
||||||
origin_docx_path = f"{os.path.abspath('.')}/data/聊天记录/{self.contact.remark}"
|
origin_docx_path = f"{os.path.abspath('.')}/data/聊天记录/{self.contact.remark}"
|
||||||
filename = os.path.join(origin_docx_path, f"{self.contact.remark}.docx")
|
|
||||||
doc = docx.Document()
|
|
||||||
doc.styles['Normal'].font.name = u'Cambria'
|
|
||||||
doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
|
|
||||||
messages = msg_db.get_messages(self.contact.wxid, time_range=self.time_range)
|
messages = msg_db.get_messages(self.contact.wxid, time_range=self.time_range)
|
||||||
Me().save_avatar(os.path.join(f"{origin_docx_path}/avatar/{Me().wxid}.png"))
|
Me().save_avatar(os.path.join(f"{origin_docx_path}/avatar/{Me().wxid}.png"))
|
||||||
if self.contact.is_chatroom:
|
if self.contact.is_chatroom:
|
||||||
@ -303,7 +300,23 @@ class DocxExporter(ExporterBase):
|
|||||||
else:
|
else:
|
||||||
self.contact.save_avatar(os.path.join(f"{origin_docx_path}/avatar/{self.contact.wxid}.png"))
|
self.contact.save_avatar(os.path.join(f"{origin_docx_path}/avatar/{self.contact.wxid}.png"))
|
||||||
self.rangeSignal.emit(len(messages))
|
self.rangeSignal.emit(len(messages))
|
||||||
|
|
||||||
|
def newdoc():
|
||||||
|
nonlocal n, doc
|
||||||
|
doc = docx.Document()
|
||||||
|
doc.styles["Normal"].font.name = "Cambria"
|
||||||
|
doc.styles["Normal"]._element.rPr.rFonts.set(qn("w:eastAsia"), "宋体")
|
||||||
|
docs.append(doc)
|
||||||
|
n += 1
|
||||||
|
|
||||||
|
doc = None
|
||||||
|
docs = []
|
||||||
|
n = 0
|
||||||
|
index = 0
|
||||||
|
newdoc()
|
||||||
for index, message in enumerate(messages):
|
for index, message in enumerate(messages):
|
||||||
|
if index % 200 == 0 and index:
|
||||||
|
newdoc()
|
||||||
type_ = message[2]
|
type_ = message[2]
|
||||||
sub_type = message[3]
|
sub_type = message[3]
|
||||||
timestamp = message[5]
|
timestamp = message[5]
|
||||||
@ -327,7 +340,23 @@ class DocxExporter(ExporterBase):
|
|||||||
self.refermsg(doc, message)
|
self.refermsg(doc, message)
|
||||||
elif type_ == 49 and sub_type == 6 and self.message_types.get(4906):
|
elif type_ == 49 and sub_type == 6 and self.message_types.get(4906):
|
||||||
self.file(doc, message)
|
self.file(doc, message)
|
||||||
print(f"【导出 DOCX {self.contact.remark}】{index}/{len(messages)}")
|
if index % 25 == 0:
|
||||||
|
print(f"【导出 DOCX {self.contact.remark}】{index}/{len(messages)}")
|
||||||
|
if index % 25:
|
||||||
|
print(f"【导出 DOCX {self.contact.remark}】{index+1}/{len(messages)}")
|
||||||
|
filename = os.path.join(origin_docx_path, f"{self.contact.remark}.docx")
|
||||||
|
doc = docx.Document()
|
||||||
|
doc.styles["Normal"].font.name = "Cambria"
|
||||||
|
doc.styles["Normal"]._element.rPr.rFonts.set(qn("w:eastAsia"), "宋体")
|
||||||
|
# doc = Composer(doc)
|
||||||
|
# for index, dx in enumerate(docs):
|
||||||
|
# print(f"【MERGE Export DOCX {self.contact.remark}】{index}/{len(docs)}")
|
||||||
|
# doc.append(dx)
|
||||||
|
# print(f"【MERGE Export DOCX {self.contact.remark}】{len(docs)}")
|
||||||
|
doc = Composer(doc) # 针对11188条消息(56组)所测,反排比正排更快,正排65s,反排54s
|
||||||
|
for index, dx in enumerate(docs[::-1]):
|
||||||
|
print(f"【合并 DOCX {self.contact.remark}】{index+1}/{len(docs)}")
|
||||||
|
doc.insert(0, dx)
|
||||||
try:
|
try:
|
||||||
doc.save(filename)
|
doc.save(filename)
|
||||||
except PermissionError:
|
except PermissionError:
|
||||||
|
Loading…
Reference in New Issue
Block a user