Update app.py
Browse files
app.py
CHANGED
|
@@ -118,16 +118,78 @@ def extract_text_with_olefile(input_path):
|
|
| 118 |
return None, f"OLE 파일 처리 오류: {str(e)}"
|
| 119 |
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
def convert_to_html_subprocess(input_path, output_dir):
|
| 122 |
"""subprocess로 hwp5html 실행"""
|
| 123 |
output_name = "output.html"
|
| 124 |
output_path = os.path.join(output_dir, output_name)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
for cmd_path in hwp5html_paths:
|
| 133 |
try:
|
|
@@ -142,28 +204,20 @@ def convert_to_html_subprocess(input_path, output_dir):
|
|
| 142 |
print(f"hwp5html stderr: {result.stderr}")
|
| 143 |
|
| 144 |
if result.returncode == 0:
|
| 145 |
-
# 실제 생성된 파일/디렉토리 찾기
|
| 146 |
-
# 1. 지정한 output_path가 파일로 존재하는지
|
| 147 |
if os.path.isfile(output_path):
|
| 148 |
return output_path, None
|
| 149 |
-
|
| 150 |
-
# 2. output_path가 디렉토리로 존재하는지
|
| 151 |
if os.path.isdir(output_path):
|
| 152 |
return output_path, None
|
| 153 |
|
| 154 |
-
# 3. output_dir 내에서 HTML 파일 찾기
|
| 155 |
for item in os.listdir(output_dir):
|
| 156 |
item_path = os.path.join(output_dir, item)
|
| 157 |
if item.lower().endswith(('.html', '.htm', '.xhtml')) and os.path.isfile(item_path):
|
| 158 |
return item_path, None
|
| 159 |
-
|
| 160 |
-
if os.path.isdir(item_path) and item != os.path.basename(input_path).replace('.hwp', ''):
|
| 161 |
-
# 디렉토리 내에 HTML이 있는지 확인
|
| 162 |
for sub_item in os.listdir(item_path):
|
| 163 |
if sub_item.lower().endswith(('.html', '.htm', '.xhtml')):
|
| 164 |
return item_path, None
|
| 165 |
|
| 166 |
-
# 4. output_dir 자체를 반환 (내부에서 HTML 검색하도록)
|
| 167 |
return output_dir, None
|
| 168 |
|
| 169 |
except (FileNotFoundError, subprocess.TimeoutExpired):
|
|
@@ -179,11 +233,22 @@ def convert_to_odt_subprocess(input_path, output_dir):
|
|
| 179 |
"""subprocess로 hwp5odt 실행"""
|
| 180 |
output_path = os.path.join(output_dir, "output.odt")
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
for cmd_path in hwp5odt_paths:
|
| 189 |
try:
|
|
@@ -208,11 +273,24 @@ def convert_to_txt_subprocess(input_path, output_dir):
|
|
| 208 |
"""subprocess로 hwp5txt 실행"""
|
| 209 |
output_path = os.path.join(output_dir, "output.txt")
|
| 210 |
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
for cmd_path in hwp5txt_paths:
|
| 218 |
try:
|
|
@@ -236,30 +314,99 @@ def convert_to_txt_subprocess(input_path, output_dir):
|
|
| 236 |
|
| 237 |
|
| 238 |
def convert_to_xml_subprocess(input_path, output_dir):
|
| 239 |
-
"""subprocess로
|
| 240 |
output_path = os.path.join(output_dir, "output.xml")
|
| 241 |
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
for cmd_path in hwp5xml_paths:
|
| 249 |
try:
|
| 250 |
-
#
|
| 251 |
result = subprocess.run(
|
| 252 |
[cmd_path, input_path],
|
| 253 |
capture_output=True,
|
| 254 |
timeout=120
|
| 255 |
)
|
| 256 |
|
| 257 |
-
print(f"
|
| 258 |
-
print(f"
|
| 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 옵션 시도
|
|
@@ -271,15 +418,41 @@ def convert_to_xml_subprocess(input_path, output_dir):
|
|
| 271 |
)
|
| 272 |
|
| 273 |
if result.returncode == 0 and os.path.exists(output_path):
|
|
|
|
| 274 |
return output_path, None
|
| 275 |
|
| 276 |
-
except
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
continue
|
| 278 |
except Exception as e:
|
| 279 |
-
print(f"
|
| 280 |
continue
|
| 281 |
|
| 282 |
-
return None, "hwp5xml
|
| 283 |
|
| 284 |
|
| 285 |
def html_to_markdown_with_markdownify(html_content):
|
|
|
|
| 118 |
return None, f"OLE 파일 처리 오류: {str(e)}"
|
| 119 |
|
| 120 |
|
| 121 |
+
def get_hwp5_command_paths(command_name):
|
| 122 |
+
"""hwp5 명령어 경로 목록 반환"""
|
| 123 |
+
paths = [
|
| 124 |
+
command_name,
|
| 125 |
+
os.path.join(os.path.dirname(sys.executable), command_name),
|
| 126 |
+
os.path.join(SCRIPT_DIR, 'bin', command_name),
|
| 127 |
+
os.path.join(PYHWP_PATH, 'bin', command_name),
|
| 128 |
+
]
|
| 129 |
+
|
| 130 |
+
# Windows의 경우 Scripts 폴더
|
| 131 |
+
if sys.platform == 'win32':
|
| 132 |
+
paths.append(os.path.join(os.path.dirname(sys.executable), 'Scripts', command_name))
|
| 133 |
+
paths.append(os.path.join(os.path.dirname(sys.executable), 'Scripts', f'{command_name}.exe'))
|
| 134 |
+
|
| 135 |
+
return paths
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def run_hwp5_module(module_args, capture_output=True):
|
| 139 |
+
"""python -m hwp5 명령 실행"""
|
| 140 |
+
try:
|
| 141 |
+
cmd = [sys.executable, '-m', 'hwp5'] + module_args
|
| 142 |
+
result = subprocess.run(
|
| 143 |
+
cmd,
|
| 144 |
+
capture_output=capture_output,
|
| 145 |
+
timeout=120
|
| 146 |
+
)
|
| 147 |
+
return result
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"python -m hwp5 실행 오류: {e}")
|
| 150 |
+
return None
|
| 151 |
+
|
| 152 |
+
|
| 153 |
def convert_to_html_subprocess(input_path, output_dir):
|
| 154 |
"""subprocess로 hwp5html 실행"""
|
| 155 |
output_name = "output.html"
|
| 156 |
output_path = os.path.join(output_dir, output_name)
|
| 157 |
|
| 158 |
+
# 방법 1: python -m hwp5 html 실행
|
| 159 |
+
try:
|
| 160 |
+
result = subprocess.run(
|
| 161 |
+
[sys.executable, '-m', 'hwp5', 'html', '--output', output_path, input_path],
|
| 162 |
+
capture_output=True,
|
| 163 |
+
text=True,
|
| 164 |
+
timeout=120
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
print(f"hwp5 html stdout: {result.stdout}")
|
| 168 |
+
print(f"hwp5 html stderr: {result.stderr}")
|
| 169 |
+
|
| 170 |
+
if result.returncode == 0:
|
| 171 |
+
# 결과 파일/디렉토리 찾기
|
| 172 |
+
if os.path.isfile(output_path):
|
| 173 |
+
return output_path, None
|
| 174 |
+
if os.path.isdir(output_path):
|
| 175 |
+
return output_path, None
|
| 176 |
+
|
| 177 |
+
# output_dir에서 HTML 찾기
|
| 178 |
+
for item in os.listdir(output_dir):
|
| 179 |
+
item_path = os.path.join(output_dir, item)
|
| 180 |
+
if item.lower().endswith(('.html', '.htm', '.xhtml')) and os.path.isfile(item_path):
|
| 181 |
+
return item_path, None
|
| 182 |
+
if os.path.isdir(item_path):
|
| 183 |
+
for sub_item in os.listdir(item_path):
|
| 184 |
+
if sub_item.lower().endswith(('.html', '.htm', '.xhtml')):
|
| 185 |
+
return item_path, None
|
| 186 |
+
|
| 187 |
+
return output_dir, None
|
| 188 |
+
except Exception as e:
|
| 189 |
+
print(f"python -m hwp5 html 오류: {e}")
|
| 190 |
+
|
| 191 |
+
# 방법 2: hwp5html 명령어 직접 실행
|
| 192 |
+
hwp5html_paths = get_hwp5_command_paths('hwp5html')
|
| 193 |
|
| 194 |
for cmd_path in hwp5html_paths:
|
| 195 |
try:
|
|
|
|
| 204 |
print(f"hwp5html stderr: {result.stderr}")
|
| 205 |
|
| 206 |
if result.returncode == 0:
|
|
|
|
|
|
|
| 207 |
if os.path.isfile(output_path):
|
| 208 |
return output_path, None
|
|
|
|
|
|
|
| 209 |
if os.path.isdir(output_path):
|
| 210 |
return output_path, None
|
| 211 |
|
|
|
|
| 212 |
for item in os.listdir(output_dir):
|
| 213 |
item_path = os.path.join(output_dir, item)
|
| 214 |
if item.lower().endswith(('.html', '.htm', '.xhtml')) and os.path.isfile(item_path):
|
| 215 |
return item_path, None
|
| 216 |
+
if os.path.isdir(item_path):
|
|
|
|
|
|
|
| 217 |
for sub_item in os.listdir(item_path):
|
| 218 |
if sub_item.lower().endswith(('.html', '.htm', '.xhtml')):
|
| 219 |
return item_path, None
|
| 220 |
|
|
|
|
| 221 |
return output_dir, None
|
| 222 |
|
| 223 |
except (FileNotFoundError, subprocess.TimeoutExpired):
|
|
|
|
| 233 |
"""subprocess로 hwp5odt 실행"""
|
| 234 |
output_path = os.path.join(output_dir, "output.odt")
|
| 235 |
|
| 236 |
+
# 방법 1: python -m hwp5 odt 실행
|
| 237 |
+
try:
|
| 238 |
+
result = subprocess.run(
|
| 239 |
+
[sys.executable, '-m', 'hwp5', 'odt', '--output', output_path, input_path],
|
| 240 |
+
capture_output=True,
|
| 241 |
+
text=True,
|
| 242 |
+
timeout=120
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
if result.returncode == 0 and os.path.exists(output_path):
|
| 246 |
+
return output_path, None
|
| 247 |
+
except Exception as e:
|
| 248 |
+
print(f"python -m hwp5 odt 오류: {e}")
|
| 249 |
+
|
| 250 |
+
# 방법 2: hwp5odt 명령어 직접 실행
|
| 251 |
+
hwp5odt_paths = get_hwp5_command_paths('hwp5odt')
|
| 252 |
|
| 253 |
for cmd_path in hwp5odt_paths:
|
| 254 |
try:
|
|
|
|
| 273 |
"""subprocess로 hwp5txt 실행"""
|
| 274 |
output_path = os.path.join(output_dir, "output.txt")
|
| 275 |
|
| 276 |
+
# 방법 1: python -m hwp5 txt 실행
|
| 277 |
+
try:
|
| 278 |
+
result = subprocess.run(
|
| 279 |
+
[sys.executable, '-m', 'hwp5', 'txt', input_path],
|
| 280 |
+
capture_output=True,
|
| 281 |
+
text=True,
|
| 282 |
+
timeout=120
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
if result.returncode == 0 and result.stdout:
|
| 286 |
+
with open(output_path, 'w', encoding='utf-8') as f:
|
| 287 |
+
f.write(result.stdout)
|
| 288 |
+
return output_path, None
|
| 289 |
+
except Exception as e:
|
| 290 |
+
print(f"python -m hwp5 txt 오류: {e}")
|
| 291 |
+
|
| 292 |
+
# 방법 2: hwp5txt 명령어 직접 실행
|
| 293 |
+
hwp5txt_paths = get_hwp5_command_paths('hwp5txt')
|
| 294 |
|
| 295 |
for cmd_path in hwp5txt_paths:
|
| 296 |
try:
|
|
|
|
| 314 |
|
| 315 |
|
| 316 |
def convert_to_xml_subprocess(input_path, output_dir):
|
| 317 |
+
"""hwp5 Python API 또는 subprocess로 XML 변환"""
|
| 318 |
output_path = os.path.join(output_dir, "output.xml")
|
| 319 |
|
| 320 |
+
# 방법 1: hwp5.xmldump 모듈 직접 사용 (가장 권장)
|
| 321 |
+
if PYHWP_AVAILABLE:
|
| 322 |
+
try:
|
| 323 |
+
from hwp5.xmlmodel import Hwp5File as XmlHwp5File
|
| 324 |
+
from hwp5.xmldump import xmldump_flat
|
| 325 |
+
|
| 326 |
+
hwp5file = XmlHwp5File(input_path)
|
| 327 |
+
|
| 328 |
+
with open(output_path, 'wb') as f:
|
| 329 |
+
xmldump_flat(hwp5file, f)
|
| 330 |
+
|
| 331 |
+
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|
| 332 |
+
print(f"XML 변환 성공 (xmldump_flat): {output_path}")
|
| 333 |
+
return output_path, None
|
| 334 |
+
except ImportError as e:
|
| 335 |
+
print(f"xmldump 모듈 import 실패: {e}")
|
| 336 |
+
except Exception as e:
|
| 337 |
+
print(f"xmldump_flat 오류: {e}")
|
| 338 |
+
|
| 339 |
+
# 방법 2: hwp5.proc 모듈 사용
|
| 340 |
+
try:
|
| 341 |
+
from hwp5.proc import xml_command
|
| 342 |
+
from io import BytesIO
|
| 343 |
+
import sys
|
| 344 |
+
from contextlib import redirect_stdout
|
| 345 |
+
|
| 346 |
+
# XML 출력 캡처
|
| 347 |
+
output_buffer = BytesIO()
|
| 348 |
+
|
| 349 |
+
# hwp5proc xml 실행
|
| 350 |
+
old_stdout = sys.stdout
|
| 351 |
+
sys.stdout = open(output_path, 'w', encoding='utf-8')
|
| 352 |
+
try:
|
| 353 |
+
from hwp5 import proc
|
| 354 |
+
# proc.main(['xml', input_path]) 대신 직접 호출
|
| 355 |
+
sys.argv = ['hwp5proc', 'xml', input_path]
|
| 356 |
+
proc.main()
|
| 357 |
+
except SystemExit:
|
| 358 |
+
pass
|
| 359 |
+
finally:
|
| 360 |
+
sys.stdout.close()
|
| 361 |
+
sys.stdout = old_stdout
|
| 362 |
+
|
| 363 |
+
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|
| 364 |
+
print(f"XML 변환 성공 (proc.main): {output_path}")
|
| 365 |
+
return output_path, None
|
| 366 |
+
except Exception as e:
|
| 367 |
+
print(f"hwp5.proc 오류: {e}")
|
| 368 |
+
|
| 369 |
+
# 방법 3: python -m hwp5 xml 실행
|
| 370 |
+
try:
|
| 371 |
+
result = subprocess.run(
|
| 372 |
+
[sys.executable, '-m', 'hwp5', 'xml', input_path],
|
| 373 |
+
capture_output=True,
|
| 374 |
+
timeout=120
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
print(f"python -m hwp5 xml returncode: {result.returncode}")
|
| 378 |
+
print(f"python -m hwp5 xml stdout length: {len(result.stdout)}")
|
| 379 |
+
print(f"python -m hwp5 xml stderr: {result.stderr.decode('utf-8', errors='ignore')[:500]}")
|
| 380 |
+
|
| 381 |
+
if result.returncode == 0 and result.stdout:
|
| 382 |
+
with open(output_path, 'wb') as f:
|
| 383 |
+
f.write(result.stdout)
|
| 384 |
+
print(f"XML 변환 성공 (python -m): {output_path}")
|
| 385 |
+
return output_path, None
|
| 386 |
+
except subprocess.TimeoutExpired:
|
| 387 |
+
print("python -m hwp5 xml 타임아웃")
|
| 388 |
+
except Exception as e:
|
| 389 |
+
print(f"python -m hwp5 xml 오류: {e}")
|
| 390 |
+
|
| 391 |
+
# 방법 4: hwp5xml 명령어 직접 실행
|
| 392 |
+
hwp5xml_paths = get_hwp5_command_paths('hwp5xml')
|
| 393 |
|
| 394 |
for cmd_path in hwp5xml_paths:
|
| 395 |
try:
|
| 396 |
+
# stdout으로 출력
|
| 397 |
result = subprocess.run(
|
| 398 |
[cmd_path, input_path],
|
| 399 |
capture_output=True,
|
| 400 |
timeout=120
|
| 401 |
)
|
| 402 |
|
| 403 |
+
print(f"{cmd_path} returncode: {result.returncode}")
|
| 404 |
+
print(f"{cmd_path} stdout length: {len(result.stdout)}")
|
| 405 |
|
| 406 |
if result.returncode == 0 and result.stdout:
|
| 407 |
with open(output_path, 'wb') as f:
|
| 408 |
f.write(result.stdout)
|
| 409 |
+
print(f"XML 변환 성공 ({cmd_path}): {output_path}")
|
| 410 |
return output_path, None
|
| 411 |
|
| 412 |
# --output 옵션 시도
|
|
|
|
| 418 |
)
|
| 419 |
|
| 420 |
if result.returncode == 0 and os.path.exists(output_path):
|
| 421 |
+
print(f"XML 변환 성공 ({cmd_path} --output): {output_path}")
|
| 422 |
return output_path, None
|
| 423 |
|
| 424 |
+
except FileNotFoundError:
|
| 425 |
+
continue
|
| 426 |
+
except subprocess.TimeoutExpired:
|
| 427 |
+
print(f"{cmd_path} 타임아웃")
|
| 428 |
+
continue
|
| 429 |
+
except Exception as e:
|
| 430 |
+
print(f"{cmd_path} 오류: {e}")
|
| 431 |
+
continue
|
| 432 |
+
|
| 433 |
+
# 방법 5: hwp5proc xml 직접 실행
|
| 434 |
+
hwp5proc_paths = get_hwp5_command_paths('hwp5proc')
|
| 435 |
+
|
| 436 |
+
for cmd_path in hwp5proc_paths:
|
| 437 |
+
try:
|
| 438 |
+
result = subprocess.run(
|
| 439 |
+
[cmd_path, 'xml', input_path],
|
| 440 |
+
capture_output=True,
|
| 441 |
+
timeout=120
|
| 442 |
+
)
|
| 443 |
+
|
| 444 |
+
if result.returncode == 0 and result.stdout:
|
| 445 |
+
with open(output_path, 'wb') as f:
|
| 446 |
+
f.write(result.stdout)
|
| 447 |
+
print(f"XML 변환 성공 ({cmd_path} xml): {output_path}")
|
| 448 |
+
return output_path, None
|
| 449 |
+
except FileNotFoundError:
|
| 450 |
continue
|
| 451 |
except Exception as e:
|
| 452 |
+
print(f"{cmd_path} xml 오류: {e}")
|
| 453 |
continue
|
| 454 |
|
| 455 |
+
return None, "hwp5xml 변환을 수행할 수 없습니다. pyhwp 설치를 확인하세요."
|
| 456 |
|
| 457 |
|
| 458 |
def html_to_markdown_with_markdownify(html_content):
|