File size: 3,621 Bytes
6cd90ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81ae7dd
6cd90ae
 
 
 
 
81ae7dd
6cd90ae
81ae7dd
 
e48a7e5
 
 
 
19cad62
6cd90ae
 
 
 
 
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
import gradio as gr
import json
import os
import docx
from docx.oxml.ns import qn
from docx import Document
from docx.shared import Inches, Pt, Cm, Mm, RGBColor
from docx.enum.table import WD_TABLE_ALIGNMENT
import pandas as pd

with open('pokemon.json', 'r') as f:
    pokemons = json.load(f)

GEN_RANGE = {
    "1์„ธ๋Œ€": [1, 151],
    "2์„ธ๋Œ€": [152, 251],
    "3์„ธ๋Œ€": [252, 386],
    "4์„ธ๋Œ€": [387, 493],
    "5์„ธ๋Œ€": [494, 649],
    "6์„ธ๋Œ€": [650, 721],
    "7์„ธ๋Œ€": [722, 809],
    "8์„ธ๋Œ€": [810, 905],
    "9์„ธ๋Œ€": [906, 1017]
}

generation = gr.Dropdown(
            [f"{k}์„ธ๋Œ€" for k in range(1, 10)], value="1์„ธ๋Œ€", label="ํฌ์ผ“๋ชฌ ์„ธ๋Œ€", info="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜์„ธ์š”."
        )

download = gr.File(label="Download a file")
text = gr.DataFrame()

def write_docx(gen):
    filename = f'ํฌ์ผ“๋ชฌ{gen}.docx'

    document = Document()
    section = document.sections[0]
    section.page_height = Mm(297)
    section.page_width = Mm(210)
    #changing the page margins
    margin = 1.27
    sections = document.sections
    for section in sections:
        section.top_margin = Cm(margin)
        section.bottom_margin = Cm(margin)
        section.left_margin = Cm(margin)
        section.right_margin = Cm(margin)
    document.styles['Normal'].font.name = 'NanumSquareRound'
    document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), 'NanumSquareRound')

    data_dict = []
    start, end = GEN_RANGE[gen]
    for k in range(start, end+1):
        name = pokemons[k-1]['name']
        number = pokemons[k-1]['number']
        types = pokemons[k-1]['types']
        image_path = pokemons[k-1]['image_path']

        data_dict.append(
            dict(์ด๋ฆ„=name, No=number, ํƒ€์ž…='+'.join(types))
        )

        df = pd.DataFrame(data_dict)
        # Document
        table = document.add_table(rows=4, cols=1)
        table.alignment = WD_TABLE_ALIGNMENT.CENTER
        table.style = 'Table Grid'

        hdr_cells = table.rows[0].cells
        hdr_cells[0].text = f"{number}"
        hdr_cells[0].paragraphs[0].runs[0].font.size = Pt(50)
        hdr_cells[0].paragraphs[0].alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER

        hdr_cells = table.rows[1].cells
        p = hdr_cells[0].add_paragraph()
        p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER
        r = p.add_run()
        r.add_picture(image_path, width=Cm(14.5), height=Cm(14.5))
        r.add_break(docx.enum.text.WD_BREAK.LINE)

        hdr_cells = table.rows[3].cells
        hdr_cells[0].text = f"{name}"
        hdr_cells[0].paragraphs[0].runs[0].font.size = Pt(70)
        hdr_cells[0].paragraphs[0].runs[0].font.color.rgb = RGBColor(192, 192, 192)
        hdr_cells[0].paragraphs[0].alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER

        hdr_cells = table.rows[2].cells
        hdr_cells[0].text = f"{'+'.join(types)}"
        hdr_cells[0].paragraphs[0].runs[0].font.size = Pt(70)
        hdr_cells[0].paragraphs[0].runs[0].font.color.rgb = RGBColor(192, 192, 192)
        hdr_cells[0].paragraphs[0].alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER

        document.add_page_break()
        yield df[['No', '์ด๋ฆ„', 'ํƒ€์ž…']], None

    print("Saving docx file...")
    print(f"{os.listdir()}")
    document.save(filename)
    print(f"{os.listdir()}")
    return df, filename

demo = gr.Interface(write_docx, generation, [text, download], title="๋Œ€์น˜๋™ ํฌ์ผ“๋ชฌ ๋„๊ฐ ์ƒ์„ฑ๊ธฐ",
                    description="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜๊ณ , ๋‹ค์šด๋กœ๋“œ๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.")
demo.queue(concurrency_count=3)
demo.launch()