yoon-gu commited on
Commit
26d8ca3
โ€ข
1 Parent(s): f056351

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -61
app.py CHANGED
@@ -1,12 +1,13 @@
1
  import gradio as gr
2
  import json
3
- import os
4
- import docx
5
- from docx.oxml.ns import qn
6
- from docx import Document
7
- from docx.shared import Inches, Pt, Cm, Mm, RGBColor
8
- from docx.enum.table import WD_TABLE_ALIGNMENT
9
  import pandas as pd
 
 
 
 
 
 
 
10
 
11
  with open('pokemon.json', 'r') as f:
12
  pokemons = json.load(f)
@@ -27,27 +28,30 @@ generation = gr.Dropdown(
27
  [f"{k}์„ธ๋Œ€" for k in range(1, 10)], value="1์„ธ๋Œ€", label="ํฌ์ผ“๋ชฌ ์„ธ๋Œ€", info="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜์„ธ์š”."
28
  )
29
 
30
- download = gr.File(label="Download a file")
31
- text = gr.DataFrame()
32
-
33
- def write_docx(gen, progress=gr.Progress()):
34
- filename = f'ํฌ์ผ“๋ชฌ{gen}.docx'
35
-
36
- document = Document()
37
- section = document.sections[0]
38
- section.page_height = Mm(297)
39
- section.page_width = Mm(210)
40
- #changing the page margins
41
- margin = 1.27
42
- sections = document.sections
43
- for section in sections:
44
- section.top_margin = Cm(margin)
45
- section.bottom_margin = Cm(margin)
46
- section.left_margin = Cm(margin)
47
- section.right_margin = Cm(margin)
48
- document.styles['Normal'].font.name = 'NanumSquareRound'
49
- document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), 'NanumSquareRound')
50
-
 
 
 
51
  data_dict = []
52
  start, end = GEN_RANGE[gen]
53
  for k in progress.tqdm(range(start, end+1)):
@@ -60,42 +64,26 @@ def write_docx(gen, progress=gr.Progress()):
60
  dict(์ด๋ฆ„=name, No=number, ํƒ€์ž…='+'.join(types))
61
  )
62
 
63
- df = pd.DataFrame(data_dict)
64
- # Document
65
- table = document.add_table(rows=4, cols=1)
66
- table.alignment = WD_TABLE_ALIGNMENT.CENTER
67
- table.style = 'Table Grid'
68
-
69
- hdr_cells = table.rows[0].cells
70
- hdr_cells[0].text = f"{number}"
71
- hdr_cells[0].paragraphs[0].runs[0].font.size = Pt(50)
72
- hdr_cells[0].paragraphs[0].alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER
73
-
74
- hdr_cells = table.rows[1].cells
75
- p = hdr_cells[0].add_paragraph()
76
- p.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER
77
- r = p.add_run()
78
- r.add_picture(image_path, width=Cm(14.5), height=Cm(14.5))
79
- r.add_break(docx.enum.text.WD_BREAK.LINE)
80
-
81
- hdr_cells = table.rows[3].cells
82
- hdr_cells[0].text = f"{name}"
83
- hdr_cells[0].paragraphs[0].runs[0].font.size = Pt(70)
84
- hdr_cells[0].paragraphs[0].runs[0].font.color.rgb = RGBColor(192, 192, 192)
85
- hdr_cells[0].paragraphs[0].alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER
86
-
87
- hdr_cells = table.rows[2].cells
88
- hdr_cells[0].text = f"{'+'.join(types)}"
89
- hdr_cells[0].paragraphs[0].runs[0].font.size = Pt(70)
90
- hdr_cells[0].paragraphs[0].runs[0].font.color.rgb = RGBColor(192, 192, 192)
91
- hdr_cells[0].paragraphs[0].alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER
92
-
93
- document.add_page_break()
94
-
95
- document.save(filename)
96
  return df[['No', '์ด๋ฆ„', 'ํƒ€์ž…']], filename
97
 
98
- demo = gr.Interface(write_docx, generation, [text, download], title="๋Œ€์น˜๋™ ํฌ์ผ“๋ชฌ ๋„๊ฐ ์ƒ์„ฑ๊ธฐ",
99
  description="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜๊ณ , ๋‹ค์šด๋กœ๋“œ๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.")
100
  demo.queue(concurrency_count=3)
101
  demo.launch()
 
1
  import gradio as gr
2
  import json
 
 
 
 
 
 
3
  import pandas as pd
4
+ from reportlab.lib import colors
5
+ from reportlab.pdfbase import pdfmetrics
6
+ from reportlab.pdfbase.ttfonts import TTFont
7
+ from reportlab.platypus import Table, TableStyle, Image, SimpleDocTemplate, PageBreak
8
+ from reportlab.lib import colors
9
+ from reportlab.lib.pagesizes import A4
10
+ from reportlab.lib.units import cm
11
 
12
  with open('pokemon.json', 'r') as f:
13
  pokemons = json.load(f)
 
28
  [f"{k}์„ธ๋Œ€" for k in range(1, 10)], value="1์„ธ๋Œ€", label="ํฌ์ผ“๋ชฌ ์„ธ๋Œ€", info="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜์„ธ์š”."
29
  )
30
 
31
+ download = gr.File(label="ํŒŒ๋ž€ ๊ธ€์”จ๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์œผ์„ธ์š”.")
32
+ text = gr.DataFrame(label="ํฌ์ผ“๋ชฌ ๋ฆฌ์ŠคํŠธ")
33
+
34
+ def write_pdf(gen, progress=gr.Progress()):
35
+ filename = f'ํฌ์ผ“๋ชฌ{gen}.pdf'
36
+ pdfmetrics.registerFont(TTFont("๋‚˜๋ˆ”๊ณ ๋”•", "NanumGothic.ttf"))
37
+ # PDF ๋ฌธ์„œ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
38
+ pdfmetrics.registerFont(TTFont("๋‚˜๋ˆ”๊ณ ๋”•", "NanumGothic.ttf"))
39
+ doc = SimpleDocTemplate(filename, pagesize=A4)
40
+
41
+ # ํ…Œ์ด๋ธ” ์Šคํƒ€์ผ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
42
+ style = TableStyle([
43
+ ('TEXTCOLOR', (0, 1), (-1, -1), colors.gray),
44
+ ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
45
+ ('FONTNAME', (0, 0), (-1, -1), '๋‚˜๋ˆ”๊ณ ๋”•'),
46
+ ('SIZE', (0, 0), (-1, -1), 50),
47
+ ('BACKGROUND', (0, 1), (-1, 1), colors.white),
48
+ ('GRID', (0, 0), (-1, -1), 1, colors.black),
49
+ ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
50
+ ('LEADING', (0, 0), (-1, -1), 14), # ๋ผ์ธ ๊ฐ„๊ฒฉ ์กฐ์ •
51
+ ('BOTTOMPADDING', (0, 0), (-1, -1), 60),
52
+ ])
53
+
54
+ story = []
55
  data_dict = []
56
  start, end = GEN_RANGE[gen]
57
  for k in progress.tqdm(range(start, end+1)):
 
64
  dict(์ด๋ฆ„=name, No=number, ํƒ€์ž…='+'.join(types))
65
  )
66
 
67
+ # PDF์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜์„ธ์š”.
68
+ image = Image(image_path, width=13.5*cm, height=13.5*cm)
69
+
70
+ # ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.
71
+ data = [
72
+ [number],
73
+ [image], # 2๋ฒˆ์งธ ํ–‰์— ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
74
+ ['+'.join(types)],
75
+ [name],
76
+ ]
77
+ # ํ…Œ์ด๋ธ” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
78
+ table = Table(data)
79
+ table.setStyle(style)
80
+ story.append(table)
81
+
82
+ doc.build(story)
83
+ df = pd.DataFrame(data_dict)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  return df[['No', '์ด๋ฆ„', 'ํƒ€์ž…']], filename
85
 
86
+ demo = gr.Interface(write_pdf, generation, [text, download], title="๋Œ€์น˜๋™ ํฌ์ผ“๋ชฌ ๋„๊ฐ ์ƒ์„ฑ๊ธฐ",
87
  description="์›ํ•˜๋Š” ํฌ์ผ“๋ชฌ ์„ธ๋Œ€๋ฅผ ์„ ํƒํ•˜๊ณ , ๋‹ค์šด๋กœ๋“œ๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”.")
88
  demo.queue(concurrency_count=3)
89
  demo.launch()