mdm-parser 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mdm_parser-0.1.0/PKG-INFO +377 -0
- mdm_parser-0.1.0/README.md +313 -0
- mdm_parser-0.1.0/__init__.py +24 -0
- mdm_parser-0.1.0/conftest.py +40 -0
- mdm_parser-0.1.0/hwp_to_svg.py +185 -0
- mdm_parser-0.1.0/mdm_parser.egg-info/PKG-INFO +377 -0
- mdm_parser-0.1.0/mdm_parser.egg-info/SOURCES.txt +23 -0
- mdm_parser-0.1.0/mdm_parser.egg-info/dependency_links.txt +1 -0
- mdm_parser-0.1.0/mdm_parser.egg-info/entry_points.txt +4 -0
- mdm_parser-0.1.0/mdm_parser.egg-info/requires.txt +43 -0
- mdm_parser-0.1.0/mdm_parser.egg-info/top_level.txt +1 -0
- mdm_parser-0.1.0/ocr_bridge.py +888 -0
- mdm_parser-0.1.0/ocr_processor.py +173 -0
- mdm_parser-0.1.0/pdf_processor.py +94 -0
- mdm_parser-0.1.0/pyproject.toml +255 -0
- mdm_parser-0.1.0/setup.cfg +4 -0
- mdm_parser-0.1.0/tests/test_converters.py +93 -0
- mdm_parser-0.1.0/tests/test_mdm_parser.py +700 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mdm-parser
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python helpers for MDM (Markdown+Media) document conversion - HWP, PDF, DOCX parser and media renderer
|
|
5
|
+
Author-email: seunghan91 <seunghan91@users.noreply.github.com>
|
|
6
|
+
Maintainer-email: seunghan91 <seunghan91@users.noreply.github.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/seunghan91/markdown-media
|
|
9
|
+
Project-URL: Documentation, https://github.com/seunghan91/markdown-media#readme
|
|
10
|
+
Project-URL: Repository, https://github.com/seunghan91/markdown-media.git
|
|
11
|
+
Project-URL: Issues, https://github.com/seunghan91/markdown-media/issues
|
|
12
|
+
Project-URL: Changelog, https://github.com/seunghan91/markdown-media/releases
|
|
13
|
+
Keywords: markdown,media,hwp,pdf,docx,document,conversion,parser,ocr,table,chart
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Topic :: Text Processing :: Markup
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Classifier: Topic :: Multimedia :: Graphics :: Graphics Conversion
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Python: >=3.8
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
Requires-Dist: pdfplumber>=0.10.0
|
|
30
|
+
Requires-Dist: pillow>=9.0.0
|
|
31
|
+
Requires-Dist: svgwrite>=1.4.0
|
|
32
|
+
Requires-Dist: typing-extensions>=4.0.0; python_version < "3.10"
|
|
33
|
+
Provides-Extra: hwp
|
|
34
|
+
Requires-Dist: pyhwp>=0.1b12; extra == "hwp"
|
|
35
|
+
Provides-Extra: ocr
|
|
36
|
+
Requires-Dist: pytesseract>=0.3.10; extra == "ocr"
|
|
37
|
+
Requires-Dist: easyocr>=1.7.0; extra == "ocr"
|
|
38
|
+
Provides-Extra: charts
|
|
39
|
+
Requires-Dist: matplotlib>=3.8.0; extra == "charts"
|
|
40
|
+
Requires-Dist: numpy>=1.24.0; extra == "charts"
|
|
41
|
+
Provides-Extra: all
|
|
42
|
+
Requires-Dist: pyhwp>=0.1b12; extra == "all"
|
|
43
|
+
Requires-Dist: pytesseract>=0.3.10; extra == "all"
|
|
44
|
+
Requires-Dist: easyocr>=1.7.0; extra == "all"
|
|
45
|
+
Requires-Dist: matplotlib>=3.8.0; extra == "all"
|
|
46
|
+
Requires-Dist: numpy>=1.24.0; extra == "all"
|
|
47
|
+
Requires-Dist: cairosvg>=2.7.0; extra == "all"
|
|
48
|
+
Provides-Extra: dev
|
|
49
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
50
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
51
|
+
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
|
|
52
|
+
Requires-Dist: pytest-xdist>=3.0.0; extra == "dev"
|
|
53
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
54
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
55
|
+
Requires-Dist: mypy>=1.5.0; extra == "dev"
|
|
56
|
+
Requires-Dist: isort>=5.12.0; extra == "dev"
|
|
57
|
+
Requires-Dist: pre-commit>=3.0.0; extra == "dev"
|
|
58
|
+
Requires-Dist: build>=1.0.0; extra == "dev"
|
|
59
|
+
Requires-Dist: twine>=4.0.0; extra == "dev"
|
|
60
|
+
Provides-Extra: docs
|
|
61
|
+
Requires-Dist: sphinx>=7.0.0; extra == "docs"
|
|
62
|
+
Requires-Dist: sphinx-rtd-theme>=2.0.0; extra == "docs"
|
|
63
|
+
Requires-Dist: myst-parser>=2.0.0; extra == "docs"
|
|
64
|
+
|
|
65
|
+
# Python 패키지 사용 가이드
|
|
66
|
+
|
|
67
|
+
MDM Python 패키지는 HWP, PDF, HTML 문서를 처리하고 Markdown+Media 형식으로 변환합니다.
|
|
68
|
+
|
|
69
|
+
## 설치
|
|
70
|
+
|
|
71
|
+
### PyPI에서 설치 (권장)
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
pip install mdm-parser
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 소스에서 설치
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
cd packages/parser-py
|
|
81
|
+
pip install -e .
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 종속성 설치
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
pip install -r requirements.txt
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**필수 종속성:**
|
|
91
|
+
|
|
92
|
+
- `pyhwp` - HWP 파일 처리
|
|
93
|
+
- `pdfplumber` - PDF 텍스트/이미지 추출
|
|
94
|
+
- `pillow` - 이미지 처리
|
|
95
|
+
- `svgwrite` - SVG 생성
|
|
96
|
+
- `beautifulsoup4` - HTML 파싱
|
|
97
|
+
- `requests` - HTTP 요청
|
|
98
|
+
|
|
99
|
+
**선택 종속성:**
|
|
100
|
+
|
|
101
|
+
- `pytesseract` - OCR (Tesseract 엔진)
|
|
102
|
+
- `easyocr` - OCR (딥러닝 기반)
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## 모듈별 사용법
|
|
107
|
+
|
|
108
|
+
### 1. hwp_to_svg.py - HWP 표를 SVG로 변환
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from mdm_parser.hwp_to_svg import HwpToSvgConverter
|
|
112
|
+
|
|
113
|
+
# 변환기 생성
|
|
114
|
+
converter = HwpToSvgConverter('document.hwp')
|
|
115
|
+
|
|
116
|
+
# SVG로 변환
|
|
117
|
+
svg_files = converter.convert('output_dir/')
|
|
118
|
+
|
|
119
|
+
# 결과
|
|
120
|
+
# output_dir/
|
|
121
|
+
# ├── table_1.svg
|
|
122
|
+
# ├── table_2.svg
|
|
123
|
+
# └── ...
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**명령줄 사용:**
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
python hwp_to_svg.py document.hwp output_dir/
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
### 2. pdf_processor.py - PDF 텍스트/이미지 추출
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
from mdm_parser.pdf_processor import PdfProcessor
|
|
138
|
+
|
|
139
|
+
# 프로세서 생성
|
|
140
|
+
processor = PdfProcessor('report.pdf')
|
|
141
|
+
|
|
142
|
+
# 텍스트 추출
|
|
143
|
+
text = processor.extract_text()
|
|
144
|
+
print(text)
|
|
145
|
+
|
|
146
|
+
# 이미지 추출
|
|
147
|
+
images = processor.extract_images('assets/')
|
|
148
|
+
# images = [{'page': 1, 'filename': 'page1_img1.png', 'path': 'assets/page1_img1.png'}, ...]
|
|
149
|
+
|
|
150
|
+
# 메타데이터 추출
|
|
151
|
+
metadata = processor.extract_metadata()
|
|
152
|
+
# {'pages': 10, 'metadata': {...}}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
**명령줄 사용:**
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
# 텍스트 추출
|
|
159
|
+
python pdf_processor.py document.pdf
|
|
160
|
+
|
|
161
|
+
# 이미지 추출
|
|
162
|
+
python pdf_processor.py document.pdf --extract-images assets/
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
### 3. ocr_processor.py - OCR (이미지 → 텍스트)
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
from mdm_parser.ocr_processor import OcrProcessor
|
|
171
|
+
|
|
172
|
+
# OCR 프로세서 생성 (자동 엔진 선택)
|
|
173
|
+
processor = OcrProcessor(engine='auto', lang='kor+eng')
|
|
174
|
+
|
|
175
|
+
# 이미지에서 텍스트 추출
|
|
176
|
+
text = processor.extract_text('scanned_document.png')
|
|
177
|
+
print(text)
|
|
178
|
+
|
|
179
|
+
# 디렉토리 일괄 처리
|
|
180
|
+
results = processor.process_directory('images/', 'output.txt')
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
**명령줄 사용:**
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
# 단일 이미지
|
|
187
|
+
python ocr_processor.py image.png
|
|
188
|
+
|
|
189
|
+
# 디렉토리 처리
|
|
190
|
+
python ocr_processor.py --dir images/ output.txt
|
|
191
|
+
|
|
192
|
+
# 특정 엔진 지정
|
|
193
|
+
python ocr_processor.py --engine easyocr image.png
|
|
194
|
+
python ocr_processor.py --engine tesseract image.png
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
**지원 엔진:**
|
|
198
|
+
|
|
199
|
+
- `tesseract` - Tesseract OCR (빠름, 가벼움)
|
|
200
|
+
- `easyocr` - EasyOCR (정확도 높음, 딥러닝)
|
|
201
|
+
- `auto` - 자동 선택
|
|
202
|
+
|
|
203
|
+
---
|
|
204
|
+
|
|
205
|
+
## 코드 예시
|
|
206
|
+
|
|
207
|
+
### 예시 1: HWP 파일에서 텍스트 추출
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
from mdm_parser.hwp_to_svg import HwpToSvgConverter
|
|
211
|
+
|
|
212
|
+
def extract_hwp_content(hwp_path, output_dir):
|
|
213
|
+
converter = HwpToSvgConverter(hwp_path)
|
|
214
|
+
|
|
215
|
+
# 표를 SVG로 변환
|
|
216
|
+
svg_files = converter.convert(output_dir)
|
|
217
|
+
|
|
218
|
+
print(f"생성된 SVG 파일: {len(svg_files)}개")
|
|
219
|
+
for svg in svg_files:
|
|
220
|
+
print(f" - {svg}")
|
|
221
|
+
|
|
222
|
+
return svg_files
|
|
223
|
+
|
|
224
|
+
# 사용
|
|
225
|
+
extract_hwp_content('report.hwp', './output/')
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### 예시 2: PDF 보고서 처리
|
|
229
|
+
|
|
230
|
+
```python
|
|
231
|
+
from mdm_parser.pdf_processor import PdfProcessor
|
|
232
|
+
import json
|
|
233
|
+
|
|
234
|
+
def process_pdf_report(pdf_path):
|
|
235
|
+
processor = PdfProcessor(pdf_path)
|
|
236
|
+
|
|
237
|
+
# 텍스트 추출
|
|
238
|
+
text = processor.extract_text()
|
|
239
|
+
|
|
240
|
+
# 메타데이터 추출
|
|
241
|
+
meta = processor.extract_metadata()
|
|
242
|
+
|
|
243
|
+
# 이미지 추출
|
|
244
|
+
images = processor.extract_images('./assets/')
|
|
245
|
+
|
|
246
|
+
# 결과 저장
|
|
247
|
+
result = {
|
|
248
|
+
'text': text,
|
|
249
|
+
'metadata': meta,
|
|
250
|
+
'images': images
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
with open('report_data.json', 'w', encoding='utf-8') as f:
|
|
254
|
+
json.dump(result, f, ensure_ascii=False, indent=2)
|
|
255
|
+
|
|
256
|
+
return result
|
|
257
|
+
|
|
258
|
+
# 사용
|
|
259
|
+
process_pdf_report('annual_report.pdf')
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### 예시 3: 스캔 문서 OCR
|
|
263
|
+
|
|
264
|
+
```python
|
|
265
|
+
from mdm_parser.ocr_processor import OcrProcessor
|
|
266
|
+
import os
|
|
267
|
+
|
|
268
|
+
def ocr_scanned_documents(input_dir, output_dir):
|
|
269
|
+
processor = OcrProcessor(engine='easyocr', lang='kor+eng')
|
|
270
|
+
|
|
271
|
+
os.makedirs(output_dir, exist_ok=True)
|
|
272
|
+
|
|
273
|
+
for filename in os.listdir(input_dir):
|
|
274
|
+
if filename.endswith(('.png', '.jpg', '.jpeg')):
|
|
275
|
+
image_path = os.path.join(input_dir, filename)
|
|
276
|
+
|
|
277
|
+
# OCR 수행
|
|
278
|
+
text = processor.extract_text(image_path)
|
|
279
|
+
|
|
280
|
+
# 결과 저장
|
|
281
|
+
output_file = os.path.join(output_dir, f"{filename}.txt")
|
|
282
|
+
with open(output_file, 'w', encoding='utf-8') as f:
|
|
283
|
+
f.write(text)
|
|
284
|
+
|
|
285
|
+
print(f"처리 완료: {filename}")
|
|
286
|
+
|
|
287
|
+
# 사용
|
|
288
|
+
ocr_scanned_documents('./scans/', './ocr_results/')
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
---
|
|
292
|
+
|
|
293
|
+
## 에러 처리
|
|
294
|
+
|
|
295
|
+
```python
|
|
296
|
+
from mdm_parser.pdf_processor import PdfProcessor
|
|
297
|
+
|
|
298
|
+
try:
|
|
299
|
+
processor = PdfProcessor('document.pdf')
|
|
300
|
+
text = processor.extract_text()
|
|
301
|
+
except FileNotFoundError:
|
|
302
|
+
print("파일을 찾을 수 없습니다")
|
|
303
|
+
except ValueError as e:
|
|
304
|
+
print(f"잘못된 파일 형식: {e}")
|
|
305
|
+
except ImportError:
|
|
306
|
+
print("pdfplumber가 설치되지 않았습니다")
|
|
307
|
+
print("설치: pip install pdfplumber")
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
---
|
|
311
|
+
|
|
312
|
+
## API 참조
|
|
313
|
+
|
|
314
|
+
### HwpToSvgConverter
|
|
315
|
+
|
|
316
|
+
| 메서드 | 설명 |
|
|
317
|
+
| ---------------------------------------------- | ---------------------------------------- |
|
|
318
|
+
| `__init__(file_path)` | HWP 파일 경로로 초기화 |
|
|
319
|
+
| `convert(output_dir)` | 표를 SVG로 변환, 생성된 파일 리스트 반환 |
|
|
320
|
+
| `extract_tables(hwp)` | pyhwp 객체에서 표 데이터 추출 |
|
|
321
|
+
| `render_table_to_svg(table_data, output_path)` | 표 데이터를 SVG로 렌더링 |
|
|
322
|
+
|
|
323
|
+
### PdfProcessor
|
|
324
|
+
|
|
325
|
+
| 메서드 | 설명 |
|
|
326
|
+
| ---------------------------- | ---------------------- |
|
|
327
|
+
| `__init__(file_path)` | PDF 파일 경로로 초기화 |
|
|
328
|
+
| `extract_text()` | 전체 텍스트 추출 |
|
|
329
|
+
| `extract_images(output_dir)` | 이미지 추출 및 저장 |
|
|
330
|
+
| `extract_metadata()` | 메타데이터 추출 |
|
|
331
|
+
|
|
332
|
+
### OcrProcessor
|
|
333
|
+
|
|
334
|
+
| 메서드 | 설명 |
|
|
335
|
+
| ------------------------------------------- | ---------------------- |
|
|
336
|
+
| `__init__(engine, lang)` | OCR 엔진과 언어 설정 |
|
|
337
|
+
| `extract_text(image_path)` | 이미지에서 텍스트 추출 |
|
|
338
|
+
| `process_directory(input_dir, output_file)` | 디렉토리 일괄 처리 |
|
|
339
|
+
|
|
340
|
+
---
|
|
341
|
+
|
|
342
|
+
## 문제 해결
|
|
343
|
+
|
|
344
|
+
### pyhwp 설치 오류
|
|
345
|
+
|
|
346
|
+
```bash
|
|
347
|
+
# 시스템 패키지 설치 (Ubuntu)
|
|
348
|
+
sudo apt-get install libxml2-dev libxslt1-dev
|
|
349
|
+
|
|
350
|
+
# pip 재설치
|
|
351
|
+
pip install --upgrade pyhwp
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
### Tesseract 설치
|
|
355
|
+
|
|
356
|
+
```bash
|
|
357
|
+
# macOS
|
|
358
|
+
brew install tesseract tesseract-lang
|
|
359
|
+
|
|
360
|
+
# Ubuntu
|
|
361
|
+
sudo apt-get install tesseract-ocr tesseract-ocr-kor
|
|
362
|
+
|
|
363
|
+
# Python 바인딩
|
|
364
|
+
pip install pytesseract
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### EasyOCR GPU 지원
|
|
368
|
+
|
|
369
|
+
```bash
|
|
370
|
+
# CUDA 있는 경우
|
|
371
|
+
pip install easyocr torch torchvision
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
**Author**: seunghan91
|
|
377
|
+
**Version**: 0.1.0
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# Python 패키지 사용 가이드
|
|
2
|
+
|
|
3
|
+
MDM Python 패키지는 HWP, PDF, HTML 문서를 처리하고 Markdown+Media 형식으로 변환합니다.
|
|
4
|
+
|
|
5
|
+
## 설치
|
|
6
|
+
|
|
7
|
+
### PyPI에서 설치 (권장)
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install mdm-parser
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### 소스에서 설치
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cd packages/parser-py
|
|
17
|
+
pip install -e .
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### 종속성 설치
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install -r requirements.txt
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**필수 종속성:**
|
|
27
|
+
|
|
28
|
+
- `pyhwp` - HWP 파일 처리
|
|
29
|
+
- `pdfplumber` - PDF 텍스트/이미지 추출
|
|
30
|
+
- `pillow` - 이미지 처리
|
|
31
|
+
- `svgwrite` - SVG 생성
|
|
32
|
+
- `beautifulsoup4` - HTML 파싱
|
|
33
|
+
- `requests` - HTTP 요청
|
|
34
|
+
|
|
35
|
+
**선택 종속성:**
|
|
36
|
+
|
|
37
|
+
- `pytesseract` - OCR (Tesseract 엔진)
|
|
38
|
+
- `easyocr` - OCR (딥러닝 기반)
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## 모듈별 사용법
|
|
43
|
+
|
|
44
|
+
### 1. hwp_to_svg.py - HWP 표를 SVG로 변환
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from mdm_parser.hwp_to_svg import HwpToSvgConverter
|
|
48
|
+
|
|
49
|
+
# 변환기 생성
|
|
50
|
+
converter = HwpToSvgConverter('document.hwp')
|
|
51
|
+
|
|
52
|
+
# SVG로 변환
|
|
53
|
+
svg_files = converter.convert('output_dir/')
|
|
54
|
+
|
|
55
|
+
# 결과
|
|
56
|
+
# output_dir/
|
|
57
|
+
# ├── table_1.svg
|
|
58
|
+
# ├── table_2.svg
|
|
59
|
+
# └── ...
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**명령줄 사용:**
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
python hwp_to_svg.py document.hwp output_dir/
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
### 2. pdf_processor.py - PDF 텍스트/이미지 추출
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from mdm_parser.pdf_processor import PdfProcessor
|
|
74
|
+
|
|
75
|
+
# 프로세서 생성
|
|
76
|
+
processor = PdfProcessor('report.pdf')
|
|
77
|
+
|
|
78
|
+
# 텍스트 추출
|
|
79
|
+
text = processor.extract_text()
|
|
80
|
+
print(text)
|
|
81
|
+
|
|
82
|
+
# 이미지 추출
|
|
83
|
+
images = processor.extract_images('assets/')
|
|
84
|
+
# images = [{'page': 1, 'filename': 'page1_img1.png', 'path': 'assets/page1_img1.png'}, ...]
|
|
85
|
+
|
|
86
|
+
# 메타데이터 추출
|
|
87
|
+
metadata = processor.extract_metadata()
|
|
88
|
+
# {'pages': 10, 'metadata': {...}}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
**명령줄 사용:**
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
# 텍스트 추출
|
|
95
|
+
python pdf_processor.py document.pdf
|
|
96
|
+
|
|
97
|
+
# 이미지 추출
|
|
98
|
+
python pdf_processor.py document.pdf --extract-images assets/
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
### 3. ocr_processor.py - OCR (이미지 → 텍스트)
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
from mdm_parser.ocr_processor import OcrProcessor
|
|
107
|
+
|
|
108
|
+
# OCR 프로세서 생성 (자동 엔진 선택)
|
|
109
|
+
processor = OcrProcessor(engine='auto', lang='kor+eng')
|
|
110
|
+
|
|
111
|
+
# 이미지에서 텍스트 추출
|
|
112
|
+
text = processor.extract_text('scanned_document.png')
|
|
113
|
+
print(text)
|
|
114
|
+
|
|
115
|
+
# 디렉토리 일괄 처리
|
|
116
|
+
results = processor.process_directory('images/', 'output.txt')
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**명령줄 사용:**
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
# 단일 이미지
|
|
123
|
+
python ocr_processor.py image.png
|
|
124
|
+
|
|
125
|
+
# 디렉토리 처리
|
|
126
|
+
python ocr_processor.py --dir images/ output.txt
|
|
127
|
+
|
|
128
|
+
# 특정 엔진 지정
|
|
129
|
+
python ocr_processor.py --engine easyocr image.png
|
|
130
|
+
python ocr_processor.py --engine tesseract image.png
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
**지원 엔진:**
|
|
134
|
+
|
|
135
|
+
- `tesseract` - Tesseract OCR (빠름, 가벼움)
|
|
136
|
+
- `easyocr` - EasyOCR (정확도 높음, 딥러닝)
|
|
137
|
+
- `auto` - 자동 선택
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## 코드 예시
|
|
142
|
+
|
|
143
|
+
### 예시 1: HWP 파일에서 텍스트 추출
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
from mdm_parser.hwp_to_svg import HwpToSvgConverter
|
|
147
|
+
|
|
148
|
+
def extract_hwp_content(hwp_path, output_dir):
|
|
149
|
+
converter = HwpToSvgConverter(hwp_path)
|
|
150
|
+
|
|
151
|
+
# 표를 SVG로 변환
|
|
152
|
+
svg_files = converter.convert(output_dir)
|
|
153
|
+
|
|
154
|
+
print(f"생성된 SVG 파일: {len(svg_files)}개")
|
|
155
|
+
for svg in svg_files:
|
|
156
|
+
print(f" - {svg}")
|
|
157
|
+
|
|
158
|
+
return svg_files
|
|
159
|
+
|
|
160
|
+
# 사용
|
|
161
|
+
extract_hwp_content('report.hwp', './output/')
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### 예시 2: PDF 보고서 처리
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
from mdm_parser.pdf_processor import PdfProcessor
|
|
168
|
+
import json
|
|
169
|
+
|
|
170
|
+
def process_pdf_report(pdf_path):
|
|
171
|
+
processor = PdfProcessor(pdf_path)
|
|
172
|
+
|
|
173
|
+
# 텍스트 추출
|
|
174
|
+
text = processor.extract_text()
|
|
175
|
+
|
|
176
|
+
# 메타데이터 추출
|
|
177
|
+
meta = processor.extract_metadata()
|
|
178
|
+
|
|
179
|
+
# 이미지 추출
|
|
180
|
+
images = processor.extract_images('./assets/')
|
|
181
|
+
|
|
182
|
+
# 결과 저장
|
|
183
|
+
result = {
|
|
184
|
+
'text': text,
|
|
185
|
+
'metadata': meta,
|
|
186
|
+
'images': images
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
with open('report_data.json', 'w', encoding='utf-8') as f:
|
|
190
|
+
json.dump(result, f, ensure_ascii=False, indent=2)
|
|
191
|
+
|
|
192
|
+
return result
|
|
193
|
+
|
|
194
|
+
# 사용
|
|
195
|
+
process_pdf_report('annual_report.pdf')
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 예시 3: 스캔 문서 OCR
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
from mdm_parser.ocr_processor import OcrProcessor
|
|
202
|
+
import os
|
|
203
|
+
|
|
204
|
+
def ocr_scanned_documents(input_dir, output_dir):
|
|
205
|
+
processor = OcrProcessor(engine='easyocr', lang='kor+eng')
|
|
206
|
+
|
|
207
|
+
os.makedirs(output_dir, exist_ok=True)
|
|
208
|
+
|
|
209
|
+
for filename in os.listdir(input_dir):
|
|
210
|
+
if filename.endswith(('.png', '.jpg', '.jpeg')):
|
|
211
|
+
image_path = os.path.join(input_dir, filename)
|
|
212
|
+
|
|
213
|
+
# OCR 수행
|
|
214
|
+
text = processor.extract_text(image_path)
|
|
215
|
+
|
|
216
|
+
# 결과 저장
|
|
217
|
+
output_file = os.path.join(output_dir, f"{filename}.txt")
|
|
218
|
+
with open(output_file, 'w', encoding='utf-8') as f:
|
|
219
|
+
f.write(text)
|
|
220
|
+
|
|
221
|
+
print(f"처리 완료: {filename}")
|
|
222
|
+
|
|
223
|
+
# 사용
|
|
224
|
+
ocr_scanned_documents('./scans/', './ocr_results/')
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## 에러 처리
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
from mdm_parser.pdf_processor import PdfProcessor
|
|
233
|
+
|
|
234
|
+
try:
|
|
235
|
+
processor = PdfProcessor('document.pdf')
|
|
236
|
+
text = processor.extract_text()
|
|
237
|
+
except FileNotFoundError:
|
|
238
|
+
print("파일을 찾을 수 없습니다")
|
|
239
|
+
except ValueError as e:
|
|
240
|
+
print(f"잘못된 파일 형식: {e}")
|
|
241
|
+
except ImportError:
|
|
242
|
+
print("pdfplumber가 설치되지 않았습니다")
|
|
243
|
+
print("설치: pip install pdfplumber")
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
---
|
|
247
|
+
|
|
248
|
+
## API 참조
|
|
249
|
+
|
|
250
|
+
### HwpToSvgConverter
|
|
251
|
+
|
|
252
|
+
| 메서드 | 설명 |
|
|
253
|
+
| ---------------------------------------------- | ---------------------------------------- |
|
|
254
|
+
| `__init__(file_path)` | HWP 파일 경로로 초기화 |
|
|
255
|
+
| `convert(output_dir)` | 표를 SVG로 변환, 생성된 파일 리스트 반환 |
|
|
256
|
+
| `extract_tables(hwp)` | pyhwp 객체에서 표 데이터 추출 |
|
|
257
|
+
| `render_table_to_svg(table_data, output_path)` | 표 데이터를 SVG로 렌더링 |
|
|
258
|
+
|
|
259
|
+
### PdfProcessor
|
|
260
|
+
|
|
261
|
+
| 메서드 | 설명 |
|
|
262
|
+
| ---------------------------- | ---------------------- |
|
|
263
|
+
| `__init__(file_path)` | PDF 파일 경로로 초기화 |
|
|
264
|
+
| `extract_text()` | 전체 텍스트 추출 |
|
|
265
|
+
| `extract_images(output_dir)` | 이미지 추출 및 저장 |
|
|
266
|
+
| `extract_metadata()` | 메타데이터 추출 |
|
|
267
|
+
|
|
268
|
+
### OcrProcessor
|
|
269
|
+
|
|
270
|
+
| 메서드 | 설명 |
|
|
271
|
+
| ------------------------------------------- | ---------------------- |
|
|
272
|
+
| `__init__(engine, lang)` | OCR 엔진과 언어 설정 |
|
|
273
|
+
| `extract_text(image_path)` | 이미지에서 텍스트 추출 |
|
|
274
|
+
| `process_directory(input_dir, output_file)` | 디렉토리 일괄 처리 |
|
|
275
|
+
|
|
276
|
+
---
|
|
277
|
+
|
|
278
|
+
## 문제 해결
|
|
279
|
+
|
|
280
|
+
### pyhwp 설치 오류
|
|
281
|
+
|
|
282
|
+
```bash
|
|
283
|
+
# 시스템 패키지 설치 (Ubuntu)
|
|
284
|
+
sudo apt-get install libxml2-dev libxslt1-dev
|
|
285
|
+
|
|
286
|
+
# pip 재설치
|
|
287
|
+
pip install --upgrade pyhwp
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Tesseract 설치
|
|
291
|
+
|
|
292
|
+
```bash
|
|
293
|
+
# macOS
|
|
294
|
+
brew install tesseract tesseract-lang
|
|
295
|
+
|
|
296
|
+
# Ubuntu
|
|
297
|
+
sudo apt-get install tesseract-ocr tesseract-ocr-kor
|
|
298
|
+
|
|
299
|
+
# Python 바인딩
|
|
300
|
+
pip install pytesseract
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### EasyOCR GPU 지원
|
|
304
|
+
|
|
305
|
+
```bash
|
|
306
|
+
# CUDA 있는 경우
|
|
307
|
+
pip install easyocr torch torchvision
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
---
|
|
311
|
+
|
|
312
|
+
**Author**: seunghan91
|
|
313
|
+
**Version**: 0.1.0
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""
|
|
2
|
+
MDM Python Parser Package
|
|
3
|
+
Provides OCR processing, PDF handling, and document conversion utilities.
|
|
4
|
+
"""
|
|
5
|
+
__version__ = '0.1.0'
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
from .ocr_processor import OcrProcessor
|
|
9
|
+
from .ocr_bridge import RustOcrBridge, OcrResult, RustOutput, OpenRouterOcrEngine
|
|
10
|
+
from .pdf_processor import PdfProcessor
|
|
11
|
+
from .hwp_to_svg import HwpToSvgConverter
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
'OcrProcessor',
|
|
15
|
+
'RustOcrBridge',
|
|
16
|
+
'OcrResult',
|
|
17
|
+
'RustOutput',
|
|
18
|
+
'OpenRouterOcrEngine',
|
|
19
|
+
'PdfProcessor',
|
|
20
|
+
'HwpToSvgConverter',
|
|
21
|
+
]
|
|
22
|
+
except ImportError:
|
|
23
|
+
# 패키지 외부에서 단독 임포트 시 (예: pytest 환경) 무시
|
|
24
|
+
__all__ = []
|