File size: 26,756 Bytes
c9e13b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import io
import docx
import configparser
import pandas as pd
import asyncio
from docx import Document
from docxtpl import DocxTemplate
from docx.shared import Pt
from docx.opc.constants import RELATIONSHIP_TYPE as RT
from docx.enum.dml import MSO_THEME_COLOR_INDEX
from docx.enum.style import WD_STYLE_TYPE
from docx.shared import Cm, Inches
from docx.oxml.shared import OxmlElement
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx.shared import RGBColor
from docx.enum.text import WD_COLOR_INDEX
from requests_toolbelt import MultipartEncoder
from datetime import datetime, timedelta

def count_values(df, col_name):
        value_counts = df[col_name].value_counts()
        result_df = pd.DataFrame(value_counts)
        result_df.columns = ['count']
        result_df.reset_index(inplace=True)
        result_df.rename(columns={'index': col_name}, inplace=True)
        return result_df


def add_hyperlink(paragraph, url, text):
        """
        A function that places a hyperlink within a paragraph object.

        :param paragraph: The paragraph we are adding the hyperlink to.
        :param url: A string containing the required url
        :param text: The text displayed for the url
        :return: A Run object containing the hyperlink
        """

        # This gets access to the document.xml.rels file and gets a new relation id value
        part = paragraph.part
        r_id = part.relate_to(url, RT.HYPERLINK, is_external=True)

        # Create the w:hyperlink tag and add needed values
        hyperlink = OxmlElement('w:hyperlink')
        hyperlink.set(qn('r:id'), r_id, )
        hyperlink.set(qn('w:history'), '1')

        # Create a w:r element
        new_run = OxmlElement('w:r')

        # Create a new w:rPr element
        rPr = OxmlElement('w:rPr')

        # Create a w:rStyle element, note this currently does not add the hyperlink style as its not in
        # the default template, I have left it here in case someone uses one that has the style in it
        rStyle = OxmlElement('w:rStyle')
        rStyle.set(qn('w:val'), 'Hyperlink')

        # Join all the xml elements together add add the required text to the w:r element
        rPr.append(rStyle)
        new_run.append(rPr)
        new_run.text = text
        hyperlink.append(new_run)

        # Create a new Run object and add the hyperlink into it
        r = paragraph.add_run()
        r._r.append(hyperlink)

        # A workaround for the lack of a hyperlink style (doesn't go purple after using the link)
        # Delete this if using a template that has the hyperlink style in it
        r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK
        r.font.underline = True

        return r



def create_table(document,count_df1):
    
        table = document.add_table(rows=2, cols=2)

        # 设置表格宽度
        table.columns[0].width = docx.shared.Inches(3.7)
        table.columns[1].width = docx.shared.Inches(3.7)

        # 设置表格边框
        table.style = 'Table Grid'
        
        # 设置表格第一行内容
        table.rows[0].height = docx.shared.Pt(9)
        first_row_cells = table.rows[0].cells
        first_row_cells[0].text = "技术进展"
        first_row_cells[0].paragraphs[0].alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.CENTER
        first_row_cells[1].text = "业内动态"
        first_row_cells[1].paragraphs[0].alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.CENTER

        # 设置第一行字体
        font = first_row_cells[0].paragraphs[0].runs[0].font
        font.name = "思源黑体 Regular"
        first_row_cells[0].paragraphs[0].runs[0]._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        font.size = docx.shared.Pt(8)
        font.bold = True

        font = first_row_cells[1].paragraphs[0].runs[0].font
        font.name = "思源黑体 Regular"
        first_row_cells[1].paragraphs[0].runs[0]._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        font.size = docx.shared.Pt(8)
        font.bold = True

        # 设置表格第二行内容
        second_row_cells = table.rows[1].cells
        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],
                                                                count_df1[3],count_df1[4],count_df1[5],count_df1[6])
        second_row_cells[0].paragraphs[0].alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.LEFT
        
        second_row_cells[1].text = "\t大厂动态    \t{0}项\n".format(count_df1[7])
        second_row_cells[1].paragraphs[0].alignment = docx.enum.text.WD_PARAGRAPH_ALIGNMENT.LEFT
        
        # 设置第二行字体
        font = second_row_cells[0].paragraphs[0].runs[0].font
        font.name = "思源黑体 Regular"
        second_row_cells[0].paragraphs[0].runs[0]._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        font.size = docx.shared.Pt(8)
        
        font = second_row_cells[1].paragraphs[0].runs[0].font
        font.name = "思源黑体 Regular"
        second_row_cells[1].paragraphs[0].runs[0]._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        font.size = docx.shared.Pt(8)
        
        # 设置行高
        table.rows[0].height = docx.shared.Pt(9)






def 荣耀周报排版(xlsx,template):
    document = Document(template)
    df = pd.read_excel(xlsx)
    res = df.sort_values(by='领域', ascending=True)

    count_df = count_values(df, '领域')
    count_df1 = count_df.sort_values(by='领域', ascending=True)["count"]
    count_df1 = list(count_df1)
    sections = ["图像理解与生成", "计算光学", "图像处理", "机器学习前沿", "自然语言交互", "计算机视觉前沿","量子计算", "定向追踪"]

    # 开头标注时间 思源黑体 Regular 四号
    try:
        date_style = document.styles['date_range']
        date_style.font.name = "思源黑体 Regular"
        date_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        date_style.font.bold = True
        date_style.font.size = Pt(14)
    except:
        date_style = document.styles.add_style('date_range', 1)
        date_style.font.name = "思源黑体 Regular"
        date_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        date_style.font.bold = True
        date_style.font.size = Pt(14)

    # 设置标题样式  思源黑体 Bold 三号
    try:
        title_style = document.styles['title2']
        title_style.font.name = "思源黑体 Bold"
        title_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
        title_style.font.bold = True
        title_style.font.size = Pt(16)
    except:
        title_style = document.styles.add_style('title2',1)
        title_style.font.name = "思源黑体 Bold"
        title_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
        title_style.font.bold = True
        title_style.font.size = Pt(16)

    try:
        title_style = document.styles['title']
        title_style.base_style = document.styles['Heading 1']
        title_style.font.name = "思源黑体 Bold"
        title_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
        title_style.font.color.rgb=RGBColor(0,0,0)
        title_style.font.bold = True
        title_style.font.size = Pt(16)
    except:
        title_style = document.styles.add_style('title',1)
        title_style.base_style = document.styles['Heading 1']
        title_style.font.name = "思源黑体 Bold"
        title_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
        title_style.font.color.rgb=RGBColor(0,0,0)
        title_style.font.bold = True
        title_style.font.size = Pt(16)
        
        
    # 热点速览技术进展小标题  思源黑体 小五 下划线   
    try:
        tech_style = document.styles['tech_progress']
        tech_style.font.name = "思源黑体 Regular"
        tech_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        tech_style.font.bold = False
        tech_style.font.size = Pt(9)
        tech_style.font.underline = True
    except:
        tech_style = document.styles.add_style('tech_progress', 1)
        tech_style.font.name = "思源黑体 Regular"
        tech_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        tech_style.font.bold = False
        tech_style.font.size = Pt(9)
        tech_style.font.underline = True
        
        
    # 热点速览注释与详情 思源黑体 小五 
    try:
        cont_style = document.styles['content']
        cont_style.font.name = "思源黑体 Regular"
        cont_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        cont_style.font.bold = False
        cont_style.font.size = Pt(9)
        cont_style.font.color.rgb=RGBColor(89,89,89)
    except:
        cont_style = document.styles.add_style('content', 1)
        cont_style.font.name = "思源黑体 Regular"
        cont_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        cont_style.font.bold = False
        cont_style.font.size = Pt(9)
        cont_style.font.color.rgb=RGBColor(89,89,89)
        
    # 思源黑体 小四 --部分正文--段落
    try:
        part1_style = document.styles['weekly_summary']
        part1_style.font.name = "思源黑体 Regular"
        part1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        part1_style.font.bold = False
        part1_style.font.size = Pt(12)
    except:
        part1_style = document.styles.add_style('weekly_summary', 1)
        part1_style.font.name = "思源黑体 Regular"
        part1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        part1_style.font.bold = False
        part1_style.font.size = Pt(12)
        
    # 思源黑体 小四 --部分正文--字符
    try:
        part2_style = document.styles['inside_para']
        part2_style.font.name = "思源黑体 Regular"
        part2_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        part2_style.font.bold = False
        part2_style.font.size = Pt(12)
    except:
        part2_style = document.styles.add_style('inside_para', 2)
        part2_style.font.name = "思源黑体 Regular"
        part2_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        part2_style.font.bold = False
        part2_style.font.size = Pt(12)

        
        
    # 思源黑体 Regular 11号字--热点正文--段落
    try:
        part3_style = document.styles['part3_style']
        part3_style.font.name = "思源黑体 Regular"
        part3_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        part3_style.font.bold = False
        part3_style.font.size = Pt(11)
    except:
        part3_style = document.styles.add_style('part3_style', 1)
        part3_style.font.name = "思源黑体 Regular"
        part3_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        part3_style.font.bold = False
        part3_style.font.size = Pt(11)
        
        
    # 定向追踪-技术进展  思源黑体 Bold 四号
    try:
        tech1_style = document.styles['tech']
        tech1_style.font.name = "思源黑体 Bold"
        tech1_style.base_style = document.styles['Heading 1']
        tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
        tech1_style.font.color.rgb=RGBColor(0,0,0)
        tech1_style.font.bold = True
        tech1_style.font.size = Pt(14)
    except:
        tech1_style = document.styles.add_style('tech',1)
        tech1_style.font.name = "思源黑体 Bold"
        tech1_style.base_style = document.styles['Heading 1']
        tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
        tech1_style.font.color.rgb=RGBColor(0,0,0)
        tech1_style.font.bold = True
        tech1_style.font.size = Pt(14)
        
        

    try:
        tech1_style = document.styles['tech2']
        tech1_style.font.name = "思源黑体 Bold"
        tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
        tech1_style.font.bold = True
        tech1_style.font.size = Pt(14)
    except:
        tech1_style = document.styles.add_style('tech2',1)
        tech1_style.font.name = "思源黑体 Bold"
        tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
        tech1_style.font.bold = True
        tech1_style.font.size = Pt(14)
        
        
        

    # 定向追踪-技术进展  思源黑体 Regular 小四号,背景灰色-25%
    try:
        tech1_style = document.styles['tech1']
        tech1_style.font.name = "思源黑体 Regular"
        tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        tech1_style.font.bold = True
        tech1_style.font.size = Pt(12)
        tech1_style.font.highlight_color=WD_COLOR_INDEX.GRAY_25

    except:
        tech1_style = document.styles.add_style('tech1',1)
        tech1_style.font.name = "思源黑体 Regular"
        tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        tech1_style.font.bold = True
        tech1_style.font.size = Pt(12)
        tech1_style.font.highlight_color=WD_COLOR_INDEX.GRAY_25


    # 思源黑体 Bold 小四 --定向追踪标题时间--段落
    try:
        part4_style = document.styles['title_date']
        part4_style.font.name = "思源黑体 Bold"
        part4_style._element.rPr.rFonts.set(qn('w:eastAsia'),"思源黑体 Bold")
        part4_style.font.bold = False
        part4_style.font.size = Pt(12)
    except:
        part4_style = document.styles.add_style('title_date', 1)
        part4_style.font.name = "思源黑体 Bold"
        part4_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Bold")
        part4_style.font.bold = False
        part4_style.font.size = Pt(12)

        
    # 思源黑体 Light 10 --定向追踪技术--段落
    try:
        part4_style = document.styles['tech_detail']
        part4_style.font.name = "思源黑体 Light"
        part4_style._element.rPr.rFonts.set(qn('w:eastAsia'),"思源黑体 Light")
        part4_style.font.bold = False
        part4_style.font.size = Pt(10)
    except:
        part4_style = document.styles.add_style('tech_detail', 1)
        part4_style.font.name = "思源黑体 Light"
        part4_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Light")
        part4_style.font.bold = False
        part4_style.font.size = Pt(10)
        
        
        
    # 定向追踪-专家点评  思源黑体 Regular 小四号
    try:
        tech1_style = document.styles['expert']
        tech1_style.font.name = "思源黑体 Regular"
        tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        tech1_style.font.bold = True
        tech1_style.font.size = Pt(12)

    except:
        tech1_style = document.styles.add_style('expert',1)
        tech1_style.font.name = "思源黑体 Regular"
        tech1_style._element.rPr.rFonts.set(qn('w:eastAsia'), "思源黑体 Regular")
        tech1_style.font.bold = True
        tech1_style.font.size = Pt(12)

    # 设置标题
    titles = ['一、本期目录', '二、热点速览', '三、定向追踪']
    t1 = "2023 年 x 月 x 日 —— 2023 年 x 月 x 日"
    t2 = "【本期荣耀周报内容概览】"
    para1 = document.add_paragraph(t1)
    para1.style = document.styles["date_range"]
    run = para1.add_run(" ")
    run.style = document.styles["inside_para"]
    run = para1.add_run(t2)
    run.style = document.styles["inside_para"]
    run.font.bold = False
    document.add_paragraph("",style = "weekly_summary")

    
        
    #一、本期目录

    document.add_paragraph(titles[0], style='title')
    document.add_paragraph("", style='title2')

    # 二、热点速览
    document.add_paragraph(titles[1], style='title')
    document.add_paragraph("", style='weekly_summary')
    document.add_paragraph("【本周期热点总结】", style='weekly_summary')
    document.add_paragraph("", style='weekly_summary')
    document.add_paragraph("以下为本周期热点速览,以事件发生时间排序。", style='weekly_summary')

    # 添加段落
    document.add_paragraph("", style='tech_progress')
    document.add_paragraph("技术进展 · 【领域】 · 【涉及技术】", style='tech_progress')
    document.add_paragraph("【技术进展正文】", style='part3_style')
    document.add_paragraph("热点注释:", style='content')
    document.add_paragraph("查看详情:", style='content')
    document.add_paragraph("", style='content')


    # 三、定向追踪
    document.add_paragraph(titles[2], style='title')
    document.add_paragraph("", style='title2')
    for section in sections:
        section2 = section
        if section != "定向追踪":
            text1 = "技术进展 · "+section2
            document.add_paragraph(text1, style='tech')
            document.add_paragraph("", style='tech2')
            document.add_paragraph("进展聚焦", style='tech1')
            document.add_paragraph("", style='tech1')
            document.add_paragraph("【占位】", style='weekly_summary')
            document.add_paragraph("", style='tech1')
            document.add_paragraph("进展详情", style='tech1')

            # 表格创建
            num = int(count_df[count_df["领域"].str.contains(section)]["count"])
            table = document.add_table(rows=num, cols=1)
        #     table.style = 'Table Grid'
            # 表格填充
            res1 = res[res["领域"].str.contains(section)].sort_values(by = "时间",ascending=False)
            for i, row in enumerate(table.rows):
                for cell in row.cells:
                    cell.text = ""
                    cell.paragraphs[0].style =  "title_date"

                    old_format = '%Y.%m.%d'
                    new_format = '%Y-%m-%d'

                    # 将日期字符串转换为 datetime 对象
                    date_str = res1.iloc[i]["时间"] 
                    date_obj = datetime.strptime(date_str, old_format)

                    # 将 datetime 对象转换为新的日期格式字符串
                    new_date_str = datetime.strftime(date_obj, new_format)
                    text1 = str(new_date_str )+ " | "+ str(res1.iloc[i]["标题"])
                    cell.add_paragraph(text1)
                    cell.paragraphs[1].style =  "title_date"

                    text2 = "· "+ str(res1.iloc[i]["涉及技术"])
                    cell.add_paragraph(text2)
                    cell.paragraphs[2].style="tech_detail"

                    text3 = str(res1.iloc[i]["简述(摘要)"])
                    cell.add_paragraph(text3)
                    cell.paragraphs[3].style = "part3_style"

                    add_hyperlink(cell.paragraphs[3], res1.iloc[i]["源链接"], "原文链接")

                    text4 = ""
                    cell.add_paragraph(text4)
                    cell.paragraphs[4].style = "part3_style"

                    if res1.iloc[i]["是否点评"] == "是":
                        text5 = "专家点评"
                        cell.add_paragraph(text5)
                        cell.paragraphs[5].style = "expert"

                        text6 = ""
                        cell.add_paragraph(text6)
                        cell.paragraphs[5].style = "expert"
                        
            document.add_page_break()
        
        elif section == "定向追踪":
            text1 = "业内动态 · "+"产品发布"
            document.add_paragraph(text1, style='tech')
            document.add_paragraph("", style='tech2')
            document.add_paragraph("进展聚焦", style='tech1')
            document.add_paragraph("", style='tech1')
            document.add_paragraph("【占位】", style='weekly_summary')
            document.add_paragraph("", style='tech1')
            document.add_paragraph("进展详情", style='tech1')

            # 表格创建
            table = document.add_table(rows=1, cols=1)
        #     table.style = 'Table Grid'
            # 表格填充
            for row in table.rows:
                for cell in row.cells:
                    cell.text = ""
                    cell.paragraphs[0].style =  "title_date"


                    # 将日期字符串转换为 datetime 对象

                    text1 = "yyyy-mm-dd"+ " | "+ "【标题占位】"
                    cell.add_paragraph(text1)
                    cell.paragraphs[1].style =  "title_date"

                    text2 = "· "+ "【涉及技术】"
                    cell.add_paragraph(text2)
                    cell.paragraphs[2].style="tech_detail"

                    text3 = "【简述(摘要)占位】"
                    cell.add_paragraph(text3)
                    cell.paragraphs[3].style = "part3_style"

                    add_hyperlink(cell.paragraphs[3], "【】", "【原文链接占位】")

                    text4 = ""
                    cell.add_paragraph(text4)
                    cell.paragraphs[4].style = "part3_style"


                    text5 = "【专家点评占位】"
                    cell.add_paragraph(text5)
                    cell.paragraphs[5].style = "expert"

                    text6 = ""
                    cell.add_paragraph(text6)
                    cell.paragraphs[5].style = "expert"

            document.add_page_break()
            text1 = "业内动态 · "+"大厂动态"
            document.add_paragraph(text1, style='tech')
            document.add_paragraph("", style='tech2')
            document.add_paragraph("进展聚焦", style='tech1')
            document.add_paragraph("", style='tech1')
            document.add_paragraph("【占位】", style='weekly_summary')
            document.add_paragraph("", style='tech1')
            document.add_paragraph("进展详情", style='tech1')

            # 表格创建
            table = document.add_table(rows=1, cols=1)
        #     table.style = 'Table Grid'
            # 表格填充
            for row in table.rows:
                for cell in row.cells:
                    cell.text = ""
                    cell.paragraphs[0].style =  "title_date"


                    # 将日期字符串转换为 datetime 对象

                    text1 = "yyyy-mm-dd"+ " | "+ "【标题占位】"
                    cell.add_paragraph(text1)
                    cell.paragraphs[1].style =  "title_date"

                    text2 = "· "+ "【涉及技术】"
                    cell.add_paragraph(text2)
                    cell.paragraphs[2].style="tech_detail"

                    text3 = "【简述(摘要)占位】"
                    cell.add_paragraph(text3)
                    cell.paragraphs[3].style = "part3_style"

                    add_hyperlink(cell.paragraphs[3], "【】", "【原文链接占位】")

                    text4 = ""
                    cell.add_paragraph(text4)
                    cell.paragraphs[4].style = "part3_style"


                    text5 = "【专家点评占位】"
                    cell.add_paragraph(text5)
                    cell.paragraphs[5].style = "expert"

                    text6 = ""
                    cell.add_paragraph(text6)
                    cell.paragraphs[5].style = "expert"
                    
            document.add_page_break()
            text1 = "业内动态 · "+"项目开源"
            document.add_paragraph(text1, style='tech')
            document.add_paragraph("", style='tech2')
            document.add_paragraph("进展聚焦", style='tech1')
            document.add_paragraph("", style='tech1')
            document.add_paragraph("【占位】", style='weekly_summary')
            document.add_paragraph("", style='tech1')
            document.add_paragraph("进展详情", style='tech1')

            # 表格创建
            table = document.add_table(rows=1, cols=1)
        #     table.style = 'Table Grid'
            # 表格填充
            for row in table.rows:
                for cell in row.cells:
                    cell.text = ""
                    cell.paragraphs[0].style =  "title_date"


                    # 将日期字符串转换为 datetime 对象

                    text1 = "yyyy-mm-dd"+ " | "+ "【标题占位】"
                    cell.add_paragraph(text1)
                    cell.paragraphs[1].style =  "title_date"

                    text2 = "· "+ "【涉及技术】"
                    cell.add_paragraph(text2)
                    cell.paragraphs[2].style="tech_detail"

                    text3 = "【简述(摘要)占位】"
                    cell.add_paragraph(text3)
                    cell.paragraphs[3].style = "part3_style"

                    add_hyperlink(cell.paragraphs[3], "【】", "【原文链接占位】")

                    text4 = ""
                    cell.add_paragraph(text4)
                    cell.paragraphs[4].style = "part3_style"


                    text5 = "【专家点评占位】"
                    cell.add_paragraph(text5)
                    cell.paragraphs[5].style = "expert"

                    text6 = ""
                    cell.add_paragraph(text6)
                    cell.paragraphs[5].style = "expert"

    
    return document


import pandas as pd
import docx
# Gradio 部分
import gradio as gr
import streamlit as st
from io import BytesIO

# def excel_to_docx(xlsx):
#     # 处理 Excel 文件并生成 docx 文件
#     document,name = 荣耀周报排版(xlsx)
#     return document.getvalue()

# 定义 Gradio 的输入和输出界面
# inputs = gr.inputs.File(label="Excel 文件", type=["file"])
# outputs = gr.outputs.File(label="docx 文件")



st.title('Translator App')
st.markdown("Translate from Docx file")
st.subheader("File Upload")

datas=st.file_uploader("Original File")
template=st.file_uploader("template File")
name=st.text_input('Enter New File Name: ')
stream = BytesIO()
if st.button(label='生成'):
    st.spinner('Waiting...')
    document= 荣耀周报排版(datas,template)
    out = document.save(stream)
    st.success("Translated")

st.download_button(label='Download Translated File',file_name=(f"{name}.docx"), data=stream.getvalue())