-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExportPostal.py
135 lines (104 loc) · 4.59 KB
/
ExportPostal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 18 16:51:29 2022
@author: Autozhz
"""
from docx import Document
import os,re,sys
from PrintLog import PrintLog
import pandas as pd
from CommomClass import CommomClass
class ExportPostal(CommomClass):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def generate_postal(self,**kwargs):
self.postal_path = kwargs.get('postal_path',None)
self.postal_template = kwargs.get('postal_template',None)
count = 0
df = self.read_file(**kwargs)
for i,row in df.iterrows():
count += self.fill_postal(row)
self.pop_msg('成功生成 【%s/%s】 张邮单,详情 df_combine.xlsx'%(count,df.shape[0]))
self.open_path(self.postal_path, **kwargs)
return df
def fill_postal(self,x):
postal_path = self.postal_path
doc = Document(self.postal_template)
doc.styles['Normal'].font.bold = True
uname = str(x['当事人'])
aname = re.search(r'[^,.,、;]*(?=[,.,、;])|\w+|$',
str(x['代理人'])).group(0) #多个名字只要一个
tel = re.search(r'[^,.,、;]*(?=[,.,、;])|\w+|$',
str(x['联系'])).group(0) #多个tel只要一个
case_number = str(x['案号'])
address = str(x['地址'])
judge = str(x['承办法官'])
lawfirm = str(x['律所'])
if not (uname and address):
PrintLog.log('>>> 案件【%s】信息不全不打印'%case_number)
return 0
text_agent = aname if aname and aname != uname else uname
text_user = '(代 %s)'%uname if aname and aname != uname else '' # 加入没agent则user不填,user变代理人
# 以下填充均对于模板sheet.doc,假如模板位置有变,这里需要修改
# 8 aname tel
# 9 uname
# 11 lawfirm
# 12 case
# 13 address
# 19 shujiyuan faguan
try:
para = doc.paragraphs[8] # find aname until tel
text = re.sub(r'\S+',text_agent,para.text) # [^\s]+(?=\s)
para.clear().add_run(text)
para = doc.paragraphs[8] # tel
text = re.sub(r'\S+$',tel,para.text) # (?<=\s)\d+
para.clear().add_run(text)
para = doc.paragraphs[9]
text = re.sub(r'[^\s].*[^\s]',text_user,para.text)
para.clear().add_run(text)
para = doc.paragraphs[11]
text = re.sub(r'\S+$',lawfirm,para.text)
para.clear().add_run(text)
para = doc.paragraphs[12]
text = re.sub(r'[^\s].*[^\s]',case_number,para.text)
para.clear().add_run(text)
para = doc.paragraphs[13]
text = re.sub(r'[^\s].*[^\s]',address,para.text)
para.clear().add_run(text)
para = doc.paragraphs[18]
text = re.sub(r'法官',judge,para.text)
para.clear().add_run(text)
except Exception as e:
PrintLog.log('邮单模板格式异常 \'%s\' ' %(e))
return 0
sheet_name = '%s_%s_%s的邮单.docx'%(case_number,text_agent,text_user)
sheet_name = re.sub(r'[\/\\\:\*\?\"\<\>]',' ',sheet_name) # keep rename legal
postal_full_path = os.path.join(postal_path,sheet_name)
if os.path.exists(postal_full_path):
# PrintLog.log('>>> 邮单已存在 <= %s'%sheet_name)
return 0
try:
doc.save(postal_full_path)
PrintLog.log('>>> 已生成邮单 => %s'%os.path.relpath(postal_full_path))
return 1
except Exception as e:
PrintLog.log('生成邮单 %s 异常 %s' %(os.path.relpath(postal_full_path),e))
return 0
if __name__ == '__main__':
from Global import *
ExportPostal1 = ExportPostal()
df = ExportPostal1.generate_postal(
df_input_name = df_expand_path,
isOpenPath = 1,
**kwargs)
#%%
# from docx import Document
# doc = Document('D:\\Python\\youdanji\\template.docx')
# doc.styles['Normal'].font.bold = True
# # 8 uname tel
# # 9 aname
# # 12 case
# # 13 address
# # 18 shujiyuan faguan
# for i,row in enumerate(doc.paragraphs):
# print(i,row.text)