|
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) |
|
|
|
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) |
|
|
|
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 |
|
|
|
if filename not in os.listdir(): |
|
document.save(filename) |
|
return df, filename |
|
|
|
demo = gr.Interface(write_docx, generation, [text, download], title="๋์น๋ ํฌ์ผ๋ชฌ ๋๊ฐ ์์ฑ๊ธฐ", |
|
description="์ํ๋ ํฌ์ผ๋ชฌ ์ธ๋๋ฅผ ์ ํํ๊ณ , ๋ค์ด๋ก๋๋ฅผ ๋๋ฌ์ฃผ์ธ์.") |
|
demo.queue(concurrency_count=3) |
|
demo.launch() |