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() |