seawolf2357 commited on
Commit
32b4471
ยท
verified ยท
1 Parent(s): 80461d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -2
app.py CHANGED
@@ -235,6 +235,53 @@ def convert_to_txt_subprocess(input_path, output_dir):
235
  return None, "hwp5txt ๋ช…๋ น์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
236
 
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  def html_to_markdown_with_markdownify(html_content):
239
  """markdownify๋ฅผ ์‚ฌ์šฉํ•œ HTML โ†’ Markdown ๋ณ€ํ™˜"""
240
  try:
@@ -771,6 +818,10 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
771
 
772
  output_path, error = convert_to_markdown(input_path, tmp_dir, progress_callback)
773
  ext = ".md"
 
 
 
 
774
 
775
  else:
776
  return None, f"โŒ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ํ˜•์‹: {output_format}", ""
@@ -811,7 +862,7 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
811
  # ๋ฏธ๋ฆฌ๋ณด๊ธฐ
812
  preview = ""
813
 
814
- if ext in ['.txt', '.md'] and os.path.isfile(final_output):
815
  try:
816
  with open(final_output, 'r', encoding='utf-8', errors='ignore') as f:
817
  preview = f.read(5000)
@@ -881,7 +932,7 @@ with gr.Blocks(title="HWP/HWPX ๋ณ€ํ™˜๊ธฐ") as demo:
881
 
882
  format_select = gr.Radio(
883
  label="๋ณ€ํ™˜ ํ˜•์‹",
884
- choices=["HTML", "ODT (OpenDocument)", "TXT (ํ…์ŠคํŠธ)", "Markdown"],
885
  value="TXT (ํ…์ŠคํŠธ)",
886
  info="์›ํ•˜๋Š” ์ถœ๋ ฅ ํ˜•์‹์„ ์„ ํƒํ•˜์„ธ์š”"
887
  )
@@ -924,6 +975,7 @@ with gr.Blocks(title="HWP/HWPX ๋ณ€ํ™˜๊ธฐ") as demo:
924
  | **ODT** | OpenDocument | LibreOffice, Google Docs |
925
  | **TXT** | ์ˆœ์ˆ˜ ํ…์ŠคํŠธ | ํ…์ŠคํŠธ ์ถ”์ถœ, ๊ฒ€์ƒ‰ |
926
  | **Markdown** | ๋งˆํฌ๋‹ค์šด ํ˜•์‹ | GitHub, ๋…ธ์…˜, ๋ธ”๋กœ๊ทธ |
 
927
 
928
  > โš ๏ธ **์ œํ•œ์‚ฌํ•ญ:** HWP v5 ํ˜•์‹(ํ•œ๊ธ€ 2007+)๋งŒ ์ง€์› | ์•”ํ˜ธํ™” ํŒŒ์ผ ๋ถˆ๊ฐ€ | ๋ณต์žกํ•œ ๋ ˆ์ด์•„์›ƒ์€ ์ผ๋ถ€ ์†์‹ค ๊ฐ€๋Šฅ
929
 
 
235
  return None, "hwp5txt ๋ช…๋ น์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
236
 
237
 
238
+ def convert_to_xml_subprocess(input_path, output_dir):
239
+ """subprocess๋กœ hwp5xml ์‹คํ–‰"""
240
+ output_path = os.path.join(output_dir, "output.xml")
241
+
242
+ hwp5xml_paths = [
243
+ 'hwp5xml',
244
+ os.path.join(SCRIPT_DIR, 'bin', 'hwp5xml'),
245
+ os.path.join(PYHWP_PATH, 'bin', 'hwp5xml'),
246
+ ]
247
+
248
+ for cmd_path in hwp5xml_paths:
249
+ try:
250
+ # hwp5xml์€ stdout์œผ๋กœ ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ --output ์˜ต์…˜ ์‚ฌ์šฉ
251
+ result = subprocess.run(
252
+ [cmd_path, input_path],
253
+ capture_output=True,
254
+ timeout=120
255
+ )
256
+
257
+ print(f"hwp5xml stdout length: {len(result.stdout)}")
258
+ print(f"hwp5xml stderr: {result.stderr.decode('utf-8', errors='ignore')[:500]}")
259
+
260
+ if result.returncode == 0 and result.stdout:
261
+ with open(output_path, 'wb') as f:
262
+ f.write(result.stdout)
263
+ return output_path, None
264
+
265
+ # --output ์˜ต์…˜ ์‹œ๋„
266
+ result = subprocess.run(
267
+ [cmd_path, '--output', output_path, input_path],
268
+ capture_output=True,
269
+ text=True,
270
+ timeout=120
271
+ )
272
+
273
+ if result.returncode == 0 and os.path.exists(output_path):
274
+ return output_path, None
275
+
276
+ except (FileNotFoundError, subprocess.TimeoutExpired):
277
+ continue
278
+ except Exception as e:
279
+ print(f"hwp5xml error: {e}")
280
+ continue
281
+
282
+ return None, "hwp5xml ๋ช…๋ น์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."
283
+
284
+
285
  def html_to_markdown_with_markdownify(html_content):
286
  """markdownify๋ฅผ ์‚ฌ์šฉํ•œ HTML โ†’ Markdown ๋ณ€ํ™˜"""
287
  try:
 
818
 
819
  output_path, error = convert_to_markdown(input_path, tmp_dir, progress_callback)
820
  ext = ".md"
821
+
822
+ elif output_format == "XML":
823
+ output_path, error = convert_to_xml_subprocess(input_path, tmp_dir)
824
+ ext = ".xml"
825
 
826
  else:
827
  return None, f"โŒ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ํ˜•์‹: {output_format}", ""
 
862
  # ๋ฏธ๋ฆฌ๋ณด๊ธฐ
863
  preview = ""
864
 
865
+ if ext in ['.txt', '.md', '.xml'] and os.path.isfile(final_output):
866
  try:
867
  with open(final_output, 'r', encoding='utf-8', errors='ignore') as f:
868
  preview = f.read(5000)
 
932
 
933
  format_select = gr.Radio(
934
  label="๋ณ€ํ™˜ ํ˜•์‹",
935
+ choices=["HTML", "ODT (OpenDocument)", "TXT (ํ…์ŠคํŠธ)", "Markdown", "XML"],
936
  value="TXT (ํ…์ŠคํŠธ)",
937
  info="์›ํ•˜๋Š” ์ถœ๋ ฅ ํ˜•์‹์„ ์„ ํƒํ•˜์„ธ์š”"
938
  )
 
975
  | **ODT** | OpenDocument | LibreOffice, Google Docs |
976
  | **TXT** | ์ˆœ์ˆ˜ ํ…์ŠคํŠธ | ํ…์ŠคํŠธ ์ถ”์ถœ, ๊ฒ€์ƒ‰ |
977
  | **Markdown** | ๋งˆํฌ๋‹ค์šด ํ˜•์‹ | GitHub, ๋…ธ์…˜, ๋ธ”๋กœ๊ทธ |
978
+ | **XML** | ๊ตฌ์กฐํ™” ๋ฐ์ดํ„ฐ | ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, ํŒŒ์‹ฑ, ๋ณ€ํ™˜ |
979
 
980
  > โš ๏ธ **์ œํ•œ์‚ฌํ•ญ:** HWP v5 ํ˜•์‹(ํ•œ๊ธ€ 2007+)๋งŒ ์ง€์› | ์•”ํ˜ธํ™” ํŒŒ์ผ ๋ถˆ๊ฐ€ | ๋ณต์žกํ•œ ๋ ˆ์ด์•„์›ƒ์€ ์ผ๋ถ€ ์†์‹ค ๊ฐ€๋Šฅ
981