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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +209 -36
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
- hwp5html_paths = [
127
- 'hwp5html',
128
- os.path.join(SCRIPT_DIR, 'bin', 'hwp5html'),
129
- os.path.join(PYHWP_PATH, 'bin', 'hwp5html'),
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
- # hwp5html์ด ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ–ˆ์„ ์ˆ˜ ์žˆ์Œ
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
- hwp5odt_paths = [
183
- 'hwp5odt',
184
- os.path.join(SCRIPT_DIR, 'bin', 'hwp5odt'),
185
- os.path.join(PYHWP_PATH, 'bin', 'hwp5odt'),
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
- hwp5txt_paths = [
212
- 'hwp5txt',
213
- os.path.join(SCRIPT_DIR, 'bin', 'hwp5txt'),
214
- os.path.join(PYHWP_PATH, 'bin', 'hwp5txt'),
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๋กœ 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 ์˜ต์…˜ ์‹œ๋„
@@ -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 (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):
 
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):