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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import docx
|
3 |
+
import configparser
|
4 |
+
import pandas as pd
|
5 |
+
import asyncio
|
6 |
+
from docx import Document
|
7 |
+
from docxtpl import DocxTemplate
|
8 |
+
from docx.shared import Pt
|
9 |
+
from docx.opc.constants import RELATIONSHIP_TYPE as RT
|
10 |
+
from docx.enum.dml import MSO_THEME_COLOR_INDEX
|
11 |
+
from docx.enum.style import WD_STYLE_TYPE
|
12 |
+
from docx.shared import Cm, Inches
|
13 |
+
from docx.oxml.shared import OxmlElement
|
14 |
+
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
15 |
+
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
|
16 |
+
from docx.oxml.ns import qn
|
17 |
+
from docx.shared import RGBColor
|
18 |
+
from docx.enum.text import WD_COLOR_INDEX
|
19 |
+
from requests_toolbelt import MultipartEncoder
|
20 |
+
from datetime import datetime, timedelta
|
21 |
+
|
22 |
+
def count_values(df, col_name):
|
23 |
+
value_counts = df[col_name].value_counts()
|
24 |
+
result_df = pd.DataFrame(value_counts)
|
25 |
+
result_df.columns = ['count']
|
26 |
+
result_df.reset_index(inplace=True)
|
27 |
+
result_df.rename(columns={'index': col_name}, inplace=True)
|
28 |
+
return result_df
|
29 |
+
|
30 |
+
|
31 |
+
def add_hyperlink(paragraph, url, text):
|
32 |
+
"""
|
33 |
+
A function that places a hyperlink within a paragraph object.
|
34 |
+
|
35 |
+
:param paragraph: The paragraph we are adding the hyperlink to.
|
36 |
+
:param url: A string containing the required url
|
37 |
+
:param text: The text displayed for the url
|
38 |
+
:return: A Run object containing the hyperlink
|
39 |
+
"""
|
40 |
+
|
41 |
+
# This gets access to the document.xml.rels file and gets a new relation id value
|
42 |
+
part = paragraph.part
|
43 |
+
r_id = part.relate_to(url, RT.HYPERLINK, is_external=True)
|
44 |
+
|
45 |
+
# Create the w:hyperlink tag and add needed values
|
46 |
+
hyperlink = OxmlElement('w:hyperlink')
|
47 |
+
hyperlink.set(qn('r:id'), r_id, )
|
48 |
+
hyperlink.set(qn('w:history'), '1')
|
49 |
+
|
50 |
+
# Create a w:r element
|
51 |
+
new_run = OxmlElement('w:r')
|
52 |
+
|
53 |
+
# Create a new w:rPr element
|
54 |
+
rPr = OxmlElement('w:rPr')
|
55 |
+
|
56 |
+
# Create a w:rStyle element, note this currently does not add the hyperlink style as its not in
|
57 |
+
# the default template, I have left it here in case someone uses one that has the style in it
|
58 |
+
rStyle = OxmlElement('w:rStyle')
|
59 |
+
rStyle.set(qn('w:val'), 'Hyperlink')
|
60 |
+
|
61 |
+
# Join all the xml elements together add add the required text to the w:r element
|
62 |
+
rPr.append(rStyle)
|
63 |
+
new_run.append(rPr)
|
64 |
+
new_run.text = text
|
65 |
+
hyperlink.append(new_run)
|
66 |
+
|
67 |
+
# Create a new Run object and add the hyperlink into it
|
68 |
+
r = paragraph.add_run()
|
69 |
+
r._r.append(hyperlink)
|
70 |
+
|
71 |
+
# A workaround for the lack of a hyperlink style (doesn't go purple after using the link)
|
72 |
+
# Delete this if using a template that has the hyperlink style in it
|
73 |
+
r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK
|
74 |
+
r.font.underline = True
|
75 |
+
|
76 |
+
return r
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
def create_table(document,count_df1):
|
81 |
+
|
82 |
+
table = document.add_table(rows=2, cols=2)
|
83 |
+
|
84 |
+
# 设置表格宽度
|
85 |
+
table.columns[0].width = docx.shared.Inches(3.7)
|
86 |
+
table.columns[1].width = docx.shared.Inches(3.7)
|
87 |
+
|
88 |
+
# 设置表格边框
|
89 |
+
table.style = 'Table Grid'
|
90 |
+
|
91 |
+
# 设置表格第一行内容
|
92 |
+
table.rows[0].height = docx.shared.Pt(9)
|
93 |
+
first_row_cells = table.rows[0].cells
|
94 |
+
first_row_cells[0].text = "技术进展"
|
95 |
+
first_row_cells[0].paragraphs[0].alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.CENTER
|
96 |
+
first_row_cells[1].text = "业内动态"
|
97 |
+
first_row_cells[1].paragraphs[0].alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.CENTER
|
98 |
+
|
99 |
+
# 设置第一行字体
|
100 |
+
font = first_row_cells[0].paragraphs[0].runs[0].font
|
101 |
+
font.name = "思源黑体 Regular"
|
102 |
+
first_row_cells[0].paragraphs[0].runs[0]._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
103 |
+
font.size = docx.shared.Pt(8)
|
104 |
+
font.bold = True
|
105 |
+
|
106 |
+
font = first_row_cells[1].paragraphs[0].runs[0].font
|
107 |
+
font.name = "思源黑体 Regular"
|
108 |
+
first_row_cells[1].paragraphs[0].runs[0]._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
109 |
+
font.size = docx.shared.Pt(8)
|
110 |
+
font.bold = True
|
111 |
+
|
112 |
+
# 设置表格第二行内容
|
113 |
+
second_row_cells = table.rows[1].cells
|
114 |
+
second_row_cells[0].text = '''\t图像理解与生成 \t{0}项\n\t计算光学 \t{1}项\n\t图像处理 \t{2}项\n\t机器学习前沿 \t{3}项\n\t自然语言交互 \t{4}项\n\t量子计算 \t{5}项\n\t计算机视觉前沿 \t{6}项'''.format(count_df1[0],count_df1[1],count_df1[2],
|
115 |
+
count_df1[3],count_df1[4],count_df1[5],count_df1[6])
|
116 |
+
second_row_cells[0].paragraphs[0].alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.LEFT
|
117 |
+
|
118 |
+
second_row_cells[1].text = "\t大厂动态 \t{0}项\n".format(count_df1[7])
|
119 |
+
second_row_cells[1].paragraphs[0].alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.LEFT
|
120 |
+
|
121 |
+
# 设置第二行字体
|
122 |
+
font = second_row_cells[0].paragraphs[0].runs[0].font
|
123 |
+
font.name = "思源黑体 Regular"
|
124 |
+
second_row_cells[0].paragraphs[0].runs[0]._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
125 |
+
font.size = docx.shared.Pt(8)
|
126 |
+
|
127 |
+
font = second_row_cells[1].paragraphs[0].runs[0].font
|
128 |
+
font.name = "思源黑体 Regular"
|
129 |
+
second_row_cells[1].paragraphs[0].runs[0]._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
130 |
+
font.size = docx.shared.Pt(8)
|
131 |
+
|
132 |
+
# 设置行高
|
133 |
+
table.rows[0].height = docx.shared.Pt(9)
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
|
138 |
+
|
139 |
+
|
140 |
+
def 荣耀周报排版(xlsx,template):
|
141 |
+
document = Document(template)
|
142 |
+
df = pd.read_excel(xlsx)
|
143 |
+
res = df.sort_values(by='领域', ascending=True)
|
144 |
+
|
145 |
+
count_df = count_values(df, '领域')
|
146 |
+
count_df1 = count_df.sort_values(by='领域', ascending=True)["count"]
|
147 |
+
count_df1 = list(count_df1)
|
148 |
+
sections = ["图像理解与生成", "计算光学", "图像处理", "机器学习前沿", "自然语言交互", "计算机视觉前沿","量子计算", "定向追踪"]
|
149 |
+
|
150 |
+
# 开头标注时间 思源黑体 Regular 四号
|
151 |
+
try:
|
152 |
+
date_style = document.styles['date_range']
|
153 |
+
date_style.font.name = "思源黑体 Regular"
|
154 |
+
date_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
155 |
+
date_style.font.bold = True
|
156 |
+
date_style.font.size = Pt(14)
|
157 |
+
except:
|
158 |
+
date_style = document.styles.add_style('date_range', 1)
|
159 |
+
date_style.font.name = "思源黑体 Regular"
|
160 |
+
date_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
161 |
+
date_style.font.bold = True
|
162 |
+
date_style.font.size = Pt(14)
|
163 |
+
|
164 |
+
# 设置标题样式 思源黑体 Bold 三号
|
165 |
+
try:
|
166 |
+
title_style = document.styles['title2']
|
167 |
+
title_style.font.name = "思源黑体 Bold"
|
168 |
+
title_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
|
169 |
+
title_style.font.bold = True
|
170 |
+
title_style.font.size = Pt(16)
|
171 |
+
except:
|
172 |
+
title_style = document.styles.add_style('title2',1)
|
173 |
+
title_style.font.name = "思源黑体 Bold"
|
174 |
+
title_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
|
175 |
+
title_style.font.bold = True
|
176 |
+
title_style.font.size = Pt(16)
|
177 |
+
|
178 |
+
try:
|
179 |
+
title_style = document.styles['title']
|
180 |
+
title_style.base_style = document.styles['Heading 1']
|
181 |
+
title_style.font.name = "思源黑体 Bold"
|
182 |
+
title_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
|
183 |
+
title_style.font.color.rgb=RGBColor(0,0,0)
|
184 |
+
title_style.font.bold = True
|
185 |
+
title_style.font.size = Pt(16)
|
186 |
+
except:
|
187 |
+
title_style = document.styles.add_style('title',1)
|
188 |
+
title_style.base_style = document.styles['Heading 1']
|
189 |
+
title_style.font.name = "思源黑体 Bold"
|
190 |
+
title_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
|
191 |
+
title_style.font.color.rgb=RGBColor(0,0,0)
|
192 |
+
title_style.font.bold = True
|
193 |
+
title_style.font.size = Pt(16)
|
194 |
+
|
195 |
+
|
196 |
+
# 热点速览技术进展小标题 思源黑体 小五 下划线
|
197 |
+
try:
|
198 |
+
tech_style = document.styles['tech_progress']
|
199 |
+
tech_style.font.name = "思源黑体 Regular"
|
200 |
+
tech_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
201 |
+
tech_style.font.bold = False
|
202 |
+
tech_style.font.size = Pt(9)
|
203 |
+
tech_style.font.underline = True
|
204 |
+
except:
|
205 |
+
tech_style = document.styles.add_style('tech_progress', 1)
|
206 |
+
tech_style.font.name = "思源黑体 Regular"
|
207 |
+
tech_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
208 |
+
tech_style.font.bold = False
|
209 |
+
tech_style.font.size = Pt(9)
|
210 |
+
tech_style.font.underline = True
|
211 |
+
|
212 |
+
|
213 |
+
# 热点速览注释与详情 思源黑体 小五
|
214 |
+
try:
|
215 |
+
cont_style = document.styles['content']
|
216 |
+
cont_style.font.name = "思源黑体 Regular"
|
217 |
+
cont_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
218 |
+
cont_style.font.bold = False
|
219 |
+
cont_style.font.size = Pt(9)
|
220 |
+
cont_style.font.color.rgb=RGBColor(89,89,89)
|
221 |
+
except:
|
222 |
+
cont_style = document.styles.add_style('content', 1)
|
223 |
+
cont_style.font.name = "思源黑体 Regular"
|
224 |
+
cont_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
225 |
+
cont_style.font.bold = False
|
226 |
+
cont_style.font.size = Pt(9)
|
227 |
+
cont_style.font.color.rgb=RGBColor(89,89,89)
|
228 |
+
|
229 |
+
# 思源黑体 小四 --部分正文--段落
|
230 |
+
try:
|
231 |
+
part1_style = document.styles['weekly_summary']
|
232 |
+
part1_style.font.name = "思源黑体 Regular"
|
233 |
+
part1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
234 |
+
part1_style.font.bold = False
|
235 |
+
part1_style.font.size = Pt(12)
|
236 |
+
except:
|
237 |
+
part1_style = document.styles.add_style('weekly_summary', 1)
|
238 |
+
part1_style.font.name = "思源黑体 Regular"
|
239 |
+
part1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
240 |
+
part1_style.font.bold = False
|
241 |
+
part1_style.font.size = Pt(12)
|
242 |
+
|
243 |
+
# 思源黑体 小四 --部分正文--字符
|
244 |
+
try:
|
245 |
+
part2_style = document.styles['inside_para']
|
246 |
+
part2_style.font.name = "思源黑体 Regular"
|
247 |
+
part2_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
248 |
+
part2_style.font.bold = False
|
249 |
+
part2_style.font.size = Pt(12)
|
250 |
+
except:
|
251 |
+
part2_style = document.styles.add_style('inside_para', 2)
|
252 |
+
part2_style.font.name = "思源黑体 Regular"
|
253 |
+
part2_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
254 |
+
part2_style.font.bold = False
|
255 |
+
part2_style.font.size = Pt(12)
|
256 |
+
|
257 |
+
|
258 |
+
|
259 |
+
# 思源黑体 Regular 11号字--热点正文--段落
|
260 |
+
try:
|
261 |
+
part3_style = document.styles['part3_style']
|
262 |
+
part3_style.font.name = "思源黑体 Regular"
|
263 |
+
part3_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
264 |
+
part3_style.font.bold = False
|
265 |
+
part3_style.font.size = Pt(11)
|
266 |
+
except:
|
267 |
+
part3_style = document.styles.add_style('part3_style', 1)
|
268 |
+
part3_style.font.name = "思源黑体 Regular"
|
269 |
+
part3_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
270 |
+
part3_style.font.bold = False
|
271 |
+
part3_style.font.size = Pt(11)
|
272 |
+
|
273 |
+
|
274 |
+
# 定向追踪-技术进展 思源黑体 Bold 四号
|
275 |
+
try:
|
276 |
+
tech1_style = document.styles['tech']
|
277 |
+
tech1_style.font.name = "思源黑体 Bold"
|
278 |
+
tech1_style.base_style = document.styles['Heading 1']
|
279 |
+
tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
|
280 |
+
tech1_style.font.color.rgb=RGBColor(0,0,0)
|
281 |
+
tech1_style.font.bold = True
|
282 |
+
tech1_style.font.size = Pt(14)
|
283 |
+
except:
|
284 |
+
tech1_style = document.styles.add_style('tech',1)
|
285 |
+
tech1_style.font.name = "思源黑体 Bold"
|
286 |
+
tech1_style.base_style = document.styles['Heading 1']
|
287 |
+
tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
|
288 |
+
tech1_style.font.color.rgb=RGBColor(0,0,0)
|
289 |
+
tech1_style.font.bold = True
|
290 |
+
tech1_style.font.size = Pt(14)
|
291 |
+
|
292 |
+
|
293 |
+
|
294 |
+
try:
|
295 |
+
tech1_style = document.styles['tech2']
|
296 |
+
tech1_style.font.name = "思源黑体 Bold"
|
297 |
+
tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
|
298 |
+
tech1_style.font.bold = True
|
299 |
+
tech1_style.font.size = Pt(14)
|
300 |
+
except:
|
301 |
+
tech1_style = document.styles.add_style('tech2',1)
|
302 |
+
tech1_style.font.name = "思源黑体 Bold"
|
303 |
+
tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
|
304 |
+
tech1_style.font.bold = True
|
305 |
+
tech1_style.font.size = Pt(14)
|
306 |
+
|
307 |
+
|
308 |
+
|
309 |
+
|
310 |
+
# 定向追踪-技术进展 思源黑体 Regular 小四号,背景灰色-25%
|
311 |
+
try:
|
312 |
+
tech1_style = document.styles['tech1']
|
313 |
+
tech1_style.font.name = "思源黑体 Regular"
|
314 |
+
tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
315 |
+
tech1_style.font.bold = True
|
316 |
+
tech1_style.font.size = Pt(12)
|
317 |
+
tech1_style.font.highlight_color=WD_COLOR_INDEX.GRAY_25
|
318 |
+
|
319 |
+
except:
|
320 |
+
tech1_style = document.styles.add_style('tech1',1)
|
321 |
+
tech1_style.font.name = "思源黑体 Regular"
|
322 |
+
tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
323 |
+
tech1_style.font.bold = True
|
324 |
+
tech1_style.font.size = Pt(12)
|
325 |
+
tech1_style.font.highlight_color=WD_COLOR_INDEX.GRAY_25
|
326 |
+
|
327 |
+
|
328 |
+
# 思源黑体 Bold 小四 --定向追踪标题时间--段落
|
329 |
+
try:
|
330 |
+
part4_style = document.styles['title_date']
|
331 |
+
part4_style.font.name = "思源黑体 Bold"
|
332 |
+
part4_style._element.rPr.rFonts.set(qn('w:eastAsia'),"思源黑体 Bold")
|
333 |
+
part4_style.font.bold = False
|
334 |
+
part4_style.font.size = Pt(12)
|
335 |
+
except:
|
336 |
+
part4_style = document.styles.add_style('title_date', 1)
|
337 |
+
part4_style.font.name = "思源黑体 Bold"
|
338 |
+
part4_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
|
339 |
+
part4_style.font.bold = False
|
340 |
+
part4_style.font.size = Pt(12)
|
341 |
+
|
342 |
+
|
343 |
+
# 思源黑体 Light 10 --定向追踪技术--段落
|
344 |
+
try:
|
345 |
+
part4_style = document.styles['tech_detail']
|
346 |
+
part4_style.font.name = "思源黑体 Light"
|
347 |
+
part4_style._element.rPr.rFonts.set(qn('w:eastAsia'),"思源黑体 Light")
|
348 |
+
part4_style.font.bold = False
|
349 |
+
part4_style.font.size = Pt(10)
|
350 |
+
except:
|
351 |
+
part4_style = document.styles.add_style('tech_detail', 1)
|
352 |
+
part4_style.font.name = "思源黑体 Light"
|
353 |
+
part4_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Light")
|
354 |
+
part4_style.font.bold = False
|
355 |
+
part4_style.font.size = Pt(10)
|
356 |
+
|
357 |
+
|
358 |
+
|
359 |
+
# 定���追踪-专家点评 思源黑体 Regular 小四号
|
360 |
+
try:
|
361 |
+
tech1_style = document.styles['expert']
|
362 |
+
tech1_style.font.name = "思源黑体 Regular"
|
363 |
+
tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
364 |
+
tech1_style.font.bold = True
|
365 |
+
tech1_style.font.size = Pt(12)
|
366 |
+
|
367 |
+
except:
|
368 |
+
tech1_style = document.styles.add_style('expert',1)
|
369 |
+
tech1_style.font.name = "思源黑体 Regular"
|
370 |
+
tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
|
371 |
+
tech1_style.font.bold = True
|
372 |
+
tech1_style.font.size = Pt(12)
|
373 |
+
|
374 |
+
# 设置标题
|
375 |
+
titles = ['一、本期目录', '二、热点速览', '三、定向追踪']
|
376 |
+
t1 = "2023 年 x 月 x 日 —— 2023 年 x 月 x 日"
|
377 |
+
t2 = "【本期荣耀周报内容概览】"
|
378 |
+
para1 = document.add_paragraph(t1)
|
379 |
+
para1.style = document.styles["date_range"]
|
380 |
+
run = para1.add_run(" ")
|
381 |
+
run.style = document.styles["inside_para"]
|
382 |
+
run = para1.add_run(t2)
|
383 |
+
run.style = document.styles["inside_para"]
|
384 |
+
run.font.bold = False
|
385 |
+
document.add_paragraph("",style = "weekly_summary")
|
386 |
+
|
387 |
+
|
388 |
+
|
389 |
+
#一、本期目录
|
390 |
+
|
391 |
+
document.add_paragraph(titles[0], style='title')
|
392 |
+
document.add_paragraph("", style='title2')
|
393 |
+
|
394 |
+
# 二、热点速览
|
395 |
+
document.add_paragraph(titles[1], style='title')
|
396 |
+
document.add_paragraph("", style='weekly_summary')
|
397 |
+
document.add_paragraph("【本周期热点总结】", style='weekly_summary')
|
398 |
+
document.add_paragraph("", style='weekly_summary')
|
399 |
+
document.add_paragraph("以下为本周期热点速览,以事件发生时间排序。", style='weekly_summary')
|
400 |
+
|
401 |
+
# 添加段落
|
402 |
+
document.add_paragraph("", style='tech_progress')
|
403 |
+
document.add_paragraph("技术进展 · 【领域】 · 【涉及技术】", style='tech_progress')
|
404 |
+
document.add_paragraph("【技术进展正文】", style='part3_style')
|
405 |
+
document.add_paragraph("热点注释:", style='content')
|
406 |
+
document.add_paragraph("查看详情:", style='content')
|
407 |
+
document.add_paragraph("", style='content')
|
408 |
+
|
409 |
+
|
410 |
+
# 三、定向追踪
|
411 |
+
document.add_paragraph(titles[2], style='title')
|
412 |
+
document.add_paragraph("", style='title2')
|
413 |
+
for section in sections:
|
414 |
+
section2 = section
|
415 |
+
if section != "定向追踪":
|
416 |
+
text1 = "技术进展 · "+section2
|
417 |
+
document.add_paragraph(text1, style='tech')
|
418 |
+
document.add_paragraph("", style='tech2')
|
419 |
+
document.add_paragraph("进展聚焦", style='tech1')
|
420 |
+
document.add_paragraph("", style='tech1')
|
421 |
+
document.add_paragraph("【占位】", style='weekly_summary')
|
422 |
+
document.add_paragraph("", style='tech1')
|
423 |
+
document.add_paragraph("进展详情", style='tech1')
|
424 |
+
|
425 |
+
# 表格创建
|
426 |
+
num = int(count_df[count_df["领域"].str.contains(section)]["count"])
|
427 |
+
table = document.add_table(rows=num, cols=1)
|
428 |
+
# table.style = 'Table Grid'
|
429 |
+
# 表格填充
|
430 |
+
res1 = res[res["领域"].str.contains(section)].sort_values(by = "时间",ascending=False)
|
431 |
+
for i, row in enumerate(table.rows):
|
432 |
+
for cell in row.cells:
|
433 |
+
cell.text = ""
|
434 |
+
cell.paragraphs[0].style = "title_date"
|
435 |
+
|
436 |
+
old_format = '%Y.%m.%d'
|
437 |
+
new_format = '%Y-%m-%d'
|
438 |
+
|
439 |
+
# 将日期字符串转换为 datetime 对象
|
440 |
+
date_str = res1.iloc[i]["时间"]
|
441 |
+
date_obj = datetime.strptime(date_str, old_format)
|
442 |
+
|
443 |
+
# 将 datetime 对象转换为新的日期格式字符串
|
444 |
+
new_date_str = datetime.strftime(date_obj, new_format)
|
445 |
+
text1 = str(new_date_str )+ " | "+ str(res1.iloc[i]["标题"])
|
446 |
+
cell.add_paragraph(text1)
|
447 |
+
cell.paragraphs[1].style = "title_date"
|
448 |
+
|
449 |
+
text2 = "· "+ str(res1.iloc[i]["涉及技术"])
|
450 |
+
cell.add_paragraph(text2)
|
451 |
+
cell.paragraphs[2].style="tech_detail"
|
452 |
+
|
453 |
+
text3 = str(res1.iloc[i]["简述(摘要)"])
|
454 |
+
cell.add_paragraph(text3)
|
455 |
+
cell.paragraphs[3].style = "part3_style"
|
456 |
+
|
457 |
+
add_hyperlink(cell.paragraphs[3], res1.iloc[i]["源链接"], "原文链接")
|
458 |
+
|
459 |
+
text4 = ""
|
460 |
+
cell.add_paragraph(text4)
|
461 |
+
cell.paragraphs[4].style = "part3_style"
|
462 |
+
|
463 |
+
if res1.iloc[i]["是否点评"] == "是":
|
464 |
+
text5 = "专家点评"
|
465 |
+
cell.add_paragraph(text5)
|
466 |
+
cell.paragraphs[5].style = "expert"
|
467 |
+
|
468 |
+
text6 = ""
|
469 |
+
cell.add_paragraph(text6)
|
470 |
+
cell.paragraphs[5].style = "expert"
|
471 |
+
|
472 |
+
document.add_page_break()
|
473 |
+
|
474 |
+
elif section == "定向追踪":
|
475 |
+
text1 = "业内动态 · "+"产品发布"
|
476 |
+
document.add_paragraph(text1, style='tech')
|
477 |
+
document.add_paragraph("", style='tech2')
|
478 |
+
document.add_paragraph("进展聚焦", style='tech1')
|
479 |
+
document.add_paragraph("", style='tech1')
|
480 |
+
document.add_paragraph("【占位】", style='weekly_summary')
|
481 |
+
document.add_paragraph("", style='tech1')
|
482 |
+
document.add_paragraph("进展详情", style='tech1')
|
483 |
+
|
484 |
+
# 表格创建
|
485 |
+
table = document.add_table(rows=1, cols=1)
|
486 |
+
# table.style = 'Table Grid'
|
487 |
+
# 表格填充
|
488 |
+
for row in table.rows:
|
489 |
+
for cell in row.cells:
|
490 |
+
cell.text = ""
|
491 |
+
cell.paragraphs[0].style = "title_date"
|
492 |
+
|
493 |
+
|
494 |
+
# 将日期字符串转换为 datetime 对象
|
495 |
+
|
496 |
+
text1 = "yyyy-mm-dd"+ " | "+ "【标题占位】"
|
497 |
+
cell.add_paragraph(text1)
|
498 |
+
cell.paragraphs[1].style = "title_date"
|
499 |
+
|
500 |
+
text2 = "· "+ "【涉及技术】"
|
501 |
+
cell.add_paragraph(text2)
|
502 |
+
cell.paragraphs[2].style="tech_detail"
|
503 |
+
|
504 |
+
text3 = "【简述(摘要)占位】"
|
505 |
+
cell.add_paragraph(text3)
|
506 |
+
cell.paragraphs[3].style = "part3_style"
|
507 |
+
|
508 |
+
add_hyperlink(cell.paragraphs[3], "【】", "【原文链接占位】")
|
509 |
+
|
510 |
+
text4 = ""
|
511 |
+
cell.add_paragraph(text4)
|
512 |
+
cell.paragraphs[4].style = "part3_style"
|
513 |
+
|
514 |
+
|
515 |
+
text5 = "【专家点评占位】"
|
516 |
+
cell.add_paragraph(text5)
|
517 |
+
cell.paragraphs[5].style = "expert"
|
518 |
+
|
519 |
+
text6 = ""
|
520 |
+
cell.add_paragraph(text6)
|
521 |
+
cell.paragraphs[5].style = "expert"
|
522 |
+
|
523 |
+
document.add_page_break()
|
524 |
+
text1 = "业内动态 · "+"大厂动态"
|
525 |
+
document.add_paragraph(text1, style='tech')
|
526 |
+
document.add_paragraph("", style='tech2')
|
527 |
+
document.add_paragraph("进展聚焦", style='tech1')
|
528 |
+
document.add_paragraph("", style='tech1')
|
529 |
+
document.add_paragraph("【占位】", style='weekly_summary')
|
530 |
+
document.add_paragraph("", style='tech1')
|
531 |
+
document.add_paragraph("进展详情", style='tech1')
|
532 |
+
|
533 |
+
# 表格创建
|
534 |
+
table = document.add_table(rows=1, cols=1)
|
535 |
+
# table.style = 'Table Grid'
|
536 |
+
# 表格填充
|
537 |
+
for row in table.rows:
|
538 |
+
for cell in row.cells:
|
539 |
+
cell.text = ""
|
540 |
+
cell.paragraphs[0].style = "title_date"
|
541 |
+
|
542 |
+
|
543 |
+
# 将日期字符串转换为 datetime 对象
|
544 |
+
|
545 |
+
text1 = "yyyy-mm-dd"+ " | "+ "【标题占位】"
|
546 |
+
cell.add_paragraph(text1)
|
547 |
+
cell.paragraphs[1].style = "title_date"
|
548 |
+
|
549 |
+
text2 = "· "+ "【涉及技术】"
|
550 |
+
cell.add_paragraph(text2)
|
551 |
+
cell.paragraphs[2].style="tech_detail"
|
552 |
+
|
553 |
+
text3 = "【简述(摘要)占位】"
|
554 |
+
cell.add_paragraph(text3)
|
555 |
+
cell.paragraphs[3].style = "part3_style"
|
556 |
+
|
557 |
+
add_hyperlink(cell.paragraphs[3], "【】", "【原文链接占位】")
|
558 |
+
|
559 |
+
text4 = ""
|
560 |
+
cell.add_paragraph(text4)
|
561 |
+
cell.paragraphs[4].style = "part3_style"
|
562 |
+
|
563 |
+
|
564 |
+
text5 = "【专家点评占位】"
|
565 |
+
cell.add_paragraph(text5)
|
566 |
+
cell.paragraphs[5].style = "expert"
|
567 |
+
|
568 |
+
text6 = ""
|
569 |
+
cell.add_paragraph(text6)
|
570 |
+
cell.paragraphs[5].style = "expert"
|
571 |
+
|
572 |
+
document.add_page_break()
|
573 |
+
text1 = "业内动态 · "+"项目开源"
|
574 |
+
document.add_paragraph(text1, style='tech')
|
575 |
+
document.add_paragraph("", style='tech2')
|
576 |
+
document.add_paragraph("进展聚焦", style='tech1')
|
577 |
+
document.add_paragraph("", style='tech1')
|
578 |
+
document.add_paragraph("【占位】", style='weekly_summary')
|
579 |
+
document.add_paragraph("", style='tech1')
|
580 |
+
document.add_paragraph("进展详情", style='tech1')
|
581 |
+
|
582 |
+
# 表格创建
|
583 |
+
table = document.add_table(rows=1, cols=1)
|
584 |
+
# table.style = 'Table Grid'
|
585 |
+
# 表格填充
|
586 |
+
for row in table.rows:
|
587 |
+
for cell in row.cells:
|
588 |
+
cell.text = ""
|
589 |
+
cell.paragraphs[0].style = "title_date"
|
590 |
+
|
591 |
+
|
592 |
+
# 将日期字符串转换为 datetime 对象
|
593 |
+
|
594 |
+
text1 = "yyyy-mm-dd"+ " | "+ "【标题占位】"
|
595 |
+
cell.add_paragraph(text1)
|
596 |
+
cell.paragraphs[1].style = "title_date"
|
597 |
+
|
598 |
+
text2 = "· "+ "【涉及技术���"
|
599 |
+
cell.add_paragraph(text2)
|
600 |
+
cell.paragraphs[2].style="tech_detail"
|
601 |
+
|
602 |
+
text3 = "【简述(摘要)占位】"
|
603 |
+
cell.add_paragraph(text3)
|
604 |
+
cell.paragraphs[3].style = "part3_style"
|
605 |
+
|
606 |
+
add_hyperlink(cell.paragraphs[3], "【】", "【原文链接占位】")
|
607 |
+
|
608 |
+
text4 = ""
|
609 |
+
cell.add_paragraph(text4)
|
610 |
+
cell.paragraphs[4].style = "part3_style"
|
611 |
+
|
612 |
+
|
613 |
+
text5 = "【专家点评占位】"
|
614 |
+
cell.add_paragraph(text5)
|
615 |
+
cell.paragraphs[5].style = "expert"
|
616 |
+
|
617 |
+
text6 = ""
|
618 |
+
cell.add_paragraph(text6)
|
619 |
+
cell.paragraphs[5].style = "expert"
|
620 |
+
|
621 |
+
|
622 |
+
return document
|
623 |
+
|
624 |
+
|
625 |
+
import pandas as pd
|
626 |
+
import docx
|
627 |
+
# Gradio 部分
|
628 |
+
import gradio as gr
|
629 |
+
import streamlit as st
|
630 |
+
from io import BytesIO
|
631 |
+
|
632 |
+
# def excel_to_docx(xlsx):
|
633 |
+
# # 处理 Excel 文件并生成 docx 文件
|
634 |
+
# document,name = 荣耀周报排版(xlsx)
|
635 |
+
# return document.getvalue()
|
636 |
+
|
637 |
+
# 定义 Gradio 的输入和输出界面
|
638 |
+
# inputs = gr.inputs.File(label="Excel 文件", type=["file"])
|
639 |
+
# outputs = gr.outputs.File(label="docx 文件")
|
640 |
+
|
641 |
+
|
642 |
+
|
643 |
+
st.title('Translator App')
|
644 |
+
st.markdown("Translate from Docx file")
|
645 |
+
st.subheader("File Upload")
|
646 |
+
|
647 |
+
datas=st.file_uploader("Original File")
|
648 |
+
template=st.file_uploader("template File")
|
649 |
+
name=st.text_input('Enter New File Name: ')
|
650 |
+
stream = BytesIO()
|
651 |
+
if st.button(label='生成'):
|
652 |
+
st.spinner('Waiting...')
|
653 |
+
document= 荣耀周报排版(datas,template)
|
654 |
+
out = document.save(stream)
|
655 |
+
st.success("Translated")
|
656 |
+
|
657 |
+
st.download_button(label='Download Translated File',file_name=(f"{name}.docx"), data=stream.getvalue())
|
658 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
requires.io
|
2 |
+
python-docx
|
3 |
+
pandas
|
4 |
+
datetime
|
5 |
+
requests_toolbelt
|