Add parse_pol177.py - PDF parser with Unstructured API
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Parse PDF document using unstructured-api on 192.168.1.103.
|
||||
Saves parsed JSON to /tmp/pol177_parsed.json.
|
||||
"""
|
||||
|
||||
import requests
|
||||
import json
|
||||
import sys
|
||||
|
||||
PDF_PATH = "/opt/documents_xk/АОХКСибцем/07 Служба Вице-президента по экономике и финансам/ДИТ/02 Регламенты деятельности/10 Положение Хранение электронных документов от 03.04.2024 № ПОЛ-177.pdf"
|
||||
API_URL = "http://192.168.1.103:8005/general/v0/general"
|
||||
|
||||
def parse_pdf():
|
||||
print("Parsing PDF...")
|
||||
with open(PDF_PATH, 'rb') as f:
|
||||
files = {'file': f}
|
||||
response = requests.post(API_URL, files=files)
|
||||
|
||||
if response.status_code != 200:
|
||||
print(f"ERROR: API returned {response.status_code}: {response.text[:500]}")
|
||||
sys.exit(1)
|
||||
|
||||
result = response.json()
|
||||
elements = result.get('elements', [])
|
||||
|
||||
print(f"Extracted {len(elements)} elements")
|
||||
|
||||
# Add page numbers
|
||||
for i, el in enumerate(elements):
|
||||
if 'page_number' in el.get('metadata', {}):
|
||||
el['page_number'] = el['metadata']['page_number']
|
||||
else:
|
||||
el['page_number'] = i + 1
|
||||
|
||||
# Save to JSON
|
||||
output = {
|
||||
'filename': '10 Положение Хранение электронных документов от 03.04.2024 № ПОЛ-177.pdf',
|
||||
'total_elements': len(elements),
|
||||
'elements': elements
|
||||
}
|
||||
|
||||
with open('/tmp/pol177_parsed.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(output, f, ensure_ascii=False, indent=2)
|
||||
|
||||
print(f"Saved {len(elements)} elements to /tmp/pol177_parsed.json")
|
||||
|
||||
# Stats
|
||||
types = {}
|
||||
for el in elements:
|
||||
t = el.get('type', 'Unknown')
|
||||
types[t] = types.get(t, 0) + 1
|
||||
print(f"Element types: {types}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
parse_pdf()
|
||||
Reference in New Issue
Block a user