Update app.py
Browse files
app.py
CHANGED
|
@@ -98,14 +98,14 @@ def extract_text_with_olefile(input_path):
|
|
| 98 |
|
| 99 |
def convert_to_html_subprocess(input_path, output_dir):
|
| 100 |
"""subprocess๋ก hwp5html ์คํ"""
|
| 101 |
-
|
|
|
|
| 102 |
|
| 103 |
# ๊ฐ๋ฅํ hwp5html ๊ฒฝ๋ก๋ค
|
| 104 |
hwp5html_paths = [
|
| 105 |
'hwp5html', # PATH์ ์๋ ๊ฒฝ์ฐ
|
| 106 |
os.path.join(SCRIPT_DIR, 'bin', 'hwp5html'),
|
| 107 |
os.path.join(PYHWP_PATH, 'bin', 'hwp5html'),
|
| 108 |
-
sys.executable.replace('python', 'hwp5html'),
|
| 109 |
]
|
| 110 |
|
| 111 |
for cmd_path in hwp5html_paths:
|
|
@@ -119,9 +119,11 @@ def convert_to_html_subprocess(input_path, output_dir):
|
|
| 119 |
|
| 120 |
if result.returncode == 0 and os.path.exists(output_path):
|
| 121 |
return output_path, None
|
|
|
|
| 122 |
except (FileNotFoundError, subprocess.TimeoutExpired):
|
| 123 |
continue
|
| 124 |
except Exception as e:
|
|
|
|
| 125 |
continue
|
| 126 |
|
| 127 |
return None, "hwp5html ๋ช
๋ น์ ์ฐพ์ ์ ์์ต๋๋ค."
|
|
@@ -228,7 +230,7 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
|
|
| 228 |
# ๋ณํ ์ํ
|
| 229 |
if output_format == "HTML":
|
| 230 |
output_path, error = convert_to_html_subprocess(input_path, tmp_dir)
|
| 231 |
-
ext = ".html"
|
| 232 |
|
| 233 |
elif output_format == "ODT (OpenDocument)":
|
| 234 |
output_path, error = convert_to_odt_subprocess(input_path, tmp_dir)
|
|
@@ -253,10 +255,21 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
|
|
| 253 |
return None, f"โ ์ง์ํ์ง ์๋ ํ์: {output_format}", ""
|
| 254 |
|
| 255 |
# ๋ณํ ์คํจ ์ฒ๋ฆฌ
|
| 256 |
-
if output_path is None
|
| 257 |
error_msg = error or "๋ณํ์ ์คํจํ์ต๋๋ค."
|
| 258 |
return None, f"โ {error_msg}", ""
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
progress(0.8, desc="ํ์ผ ์ค๋น ์ค...")
|
| 261 |
|
| 262 |
# ์ต์ข
ํ์ผ๋ช
์ค์
|
|
@@ -264,8 +277,13 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
|
|
| 264 |
final_filename = f"{base_name}{ext}"
|
| 265 |
final_output = os.path.join(tmp_dir, final_filename)
|
| 266 |
|
| 267 |
-
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
# ํ์ผ ํฌ๊ธฐ ํ์ธ
|
| 271 |
file_size = os.path.getsize(final_output)
|
|
@@ -275,7 +293,9 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
|
|
| 275 |
|
| 276 |
# ๋ฏธ๋ฆฌ๋ณด๊ธฐ (ํ
์คํธ/HTML์ธ ๊ฒฝ์ฐ)
|
| 277 |
preview = ""
|
| 278 |
-
|
|
|
|
|
|
|
| 279 |
try:
|
| 280 |
with open(final_output, 'r', encoding='utf-8', errors='ignore') as f:
|
| 281 |
preview = f.read(5000) # ์ฒ์ 5000์๋ง
|
|
@@ -283,6 +303,8 @@ def convert_hwp(file, output_format, progress=gr.Progress()):
|
|
| 283 |
preview += "\n\n... (๋ฏธ๋ฆฌ๋ณด๊ธฐ ์๋ต)"
|
| 284 |
except:
|
| 285 |
pass
|
|
|
|
|
|
|
| 286 |
|
| 287 |
return final_output, f"โ
๋ณํ ์๋ฃ: {final_filename} ({size_str})", preview
|
| 288 |
|
|
@@ -323,7 +345,9 @@ css = """
|
|
| 323 |
"""
|
| 324 |
|
| 325 |
# Gradio ์ธํฐํ์ด์ค
|
| 326 |
-
with gr.Blocks(
|
|
|
|
|
|
|
| 327 |
with gr.Column(elem_id="col-container"):
|
| 328 |
gr.Markdown("""
|
| 329 |
# ๐ HWP ํ์ผ ๋ณํ๊ธฐ
|
|
|
|
| 98 |
|
| 99 |
def convert_to_html_subprocess(input_path, output_dir):
|
| 100 |
"""subprocess๋ก hwp5html ์คํ"""
|
| 101 |
+
output_name = "output.html"
|
| 102 |
+
output_path = os.path.join(output_dir, output_name)
|
| 103 |
|
| 104 |
# ๊ฐ๋ฅํ hwp5html ๊ฒฝ๋ก๋ค
|
| 105 |
hwp5html_paths = [
|
| 106 |
'hwp5html', # PATH์ ์๋ ๊ฒฝ์ฐ
|
| 107 |
os.path.join(SCRIPT_DIR, 'bin', 'hwp5html'),
|
| 108 |
os.path.join(PYHWP_PATH, 'bin', 'hwp5html'),
|
|
|
|
| 109 |
]
|
| 110 |
|
| 111 |
for cmd_path in hwp5html_paths:
|
|
|
|
| 119 |
|
| 120 |
if result.returncode == 0 and os.path.exists(output_path):
|
| 121 |
return output_path, None
|
| 122 |
+
|
| 123 |
except (FileNotFoundError, subprocess.TimeoutExpired):
|
| 124 |
continue
|
| 125 |
except Exception as e:
|
| 126 |
+
print(f"hwp5html error: {e}")
|
| 127 |
continue
|
| 128 |
|
| 129 |
return None, "hwp5html ๋ช
๋ น์ ์ฐพ์ ์ ์์ต๋๋ค."
|
|
|
|
| 230 |
# ๋ณํ ์ํ
|
| 231 |
if output_format == "HTML":
|
| 232 |
output_path, error = convert_to_html_subprocess(input_path, tmp_dir)
|
| 233 |
+
ext = ".html" # ๋๋ ํ ๋ฆฌ์ธ ๊ฒฝ์ฐ ์๋์์ .zip์ผ๋ก ๋ณ๊ฒฝ๋จ
|
| 234 |
|
| 235 |
elif output_format == "ODT (OpenDocument)":
|
| 236 |
output_path, error = convert_to_odt_subprocess(input_path, tmp_dir)
|
|
|
|
| 255 |
return None, f"โ ์ง์ํ์ง ์๋ ํ์: {output_format}", ""
|
| 256 |
|
| 257 |
# ๋ณํ ์คํจ ์ฒ๋ฆฌ
|
| 258 |
+
if output_path is None:
|
| 259 |
error_msg = error or "๋ณํ์ ์คํจํ์ต๋๋ค."
|
| 260 |
return None, f"โ {error_msg}", ""
|
| 261 |
|
| 262 |
+
# output_path๊ฐ ๋๋ ํ ๋ฆฌ์ธ์ง ํ์ผ์ธ์ง ํ์ธ
|
| 263 |
+
if os.path.isdir(output_path):
|
| 264 |
+
# ๋๋ ํ ๋ฆฌ์ธ ๊ฒฝ์ฐ (HTML ๋ณํ ์ ๋ฐ์ ๊ฐ๋ฅ) - ZIP ์์ถ
|
| 265 |
+
zip_base = os.path.join(tmp_dir, "output_archive")
|
| 266 |
+
zip_path = shutil.make_archive(zip_base, 'zip', output_path)
|
| 267 |
+
output_path = zip_path
|
| 268 |
+
ext = ".zip"
|
| 269 |
+
|
| 270 |
+
if not os.path.exists(output_path):
|
| 271 |
+
return None, "โ ๋ณํ๋ ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค.", ""
|
| 272 |
+
|
| 273 |
progress(0.8, desc="ํ์ผ ์ค๋น ์ค...")
|
| 274 |
|
| 275 |
# ์ต์ข
ํ์ผ๋ช
์ค์
|
|
|
|
| 277 |
final_filename = f"{base_name}{ext}"
|
| 278 |
final_output = os.path.join(tmp_dir, final_filename)
|
| 279 |
|
| 280 |
+
# ํ์ผ ์ด๋ (๋ค๋ฅธ ๊ฒฝ๋ก์ผ ๋๋ง)
|
| 281 |
+
if output_path != final_output and os.path.isfile(output_path):
|
| 282 |
+
shutil.copy2(output_path, final_output)
|
| 283 |
+
elif output_path == final_output:
|
| 284 |
+
pass # ์ด๋ฏธ ๊ฐ์ ๊ฒฝ๋ก
|
| 285 |
+
else:
|
| 286 |
+
final_output = output_path # ์ด๋ ์คํจ ์ ์๋ณธ ์ฌ์ฉ
|
| 287 |
|
| 288 |
# ํ์ผ ํฌ๊ธฐ ํ์ธ
|
| 289 |
file_size = os.path.getsize(final_output)
|
|
|
|
| 293 |
|
| 294 |
# ๋ฏธ๋ฆฌ๋ณด๊ธฐ (ํ
์คํธ/HTML์ธ ๊ฒฝ์ฐ)
|
| 295 |
preview = ""
|
| 296 |
+
|
| 297 |
+
# TXT ํ์ผ๋ง ๋ฏธ๋ฆฌ๋ณด๊ธฐ (HTML์ ZIP์ด๊ฑฐ๋ ๋๋ ํ ๋ฆฌ์ผ ์ ์์)
|
| 298 |
+
if ext == '.txt' and os.path.isfile(final_output):
|
| 299 |
try:
|
| 300 |
with open(final_output, 'r', encoding='utf-8', errors='ignore') as f:
|
| 301 |
preview = f.read(5000) # ์ฒ์ 5000์๋ง
|
|
|
|
| 303 |
preview += "\n\n... (๋ฏธ๋ฆฌ๋ณด๊ธฐ ์๋ต)"
|
| 304 |
except:
|
| 305 |
pass
|
| 306 |
+
elif ext == '.zip':
|
| 307 |
+
preview = "๐ฆ HTML ๋ณํ ๊ฒฐ๊ณผ๊ฐ ZIP ํ์ผ๋ก ์์ถ๋์์ต๋๋ค.\n๋ค์ด๋ก๋ ํ ์์ถ์ ํ์ด์ HTML ํ์ผ์ ํ์ธํ์ธ์."
|
| 308 |
|
| 309 |
return final_output, f"โ
๋ณํ ์๋ฃ: {final_filename} ({size_str})", preview
|
| 310 |
|
|
|
|
| 345 |
"""
|
| 346 |
|
| 347 |
# Gradio ์ธํฐํ์ด์ค
|
| 348 |
+
with gr.Blocks(title="HWP ๋ณํ๊ธฐ") as demo:
|
| 349 |
+
# CSS ์ ์ฉ
|
| 350 |
+
gr.HTML(f"<style>{css}</style>")
|
| 351 |
with gr.Column(elem_id="col-container"):
|
| 352 |
gr.Markdown("""
|
| 353 |
# ๐ HWP ํ์ผ ๋ณํ๊ธฐ
|