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