Update app.py
Browse files
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 |
|