Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 19,014 Bytes
896453f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 | # π HANDLING MULTIPLE DOCUMENT FORMATS
**Government sites use PDFs, PowerPoint, Word, Excel, and more. Here's how to handle them ALL.**
---
## π― THE STRATEGY
**Regardless of format: Extract text β Store in Parquet**
```
PDF, PPTX, DOCX, XLSX, HTML β Extract Text β Parquet (1 file)
```
**NOT:**
```
β Store 1000 PDFs + 500 PPTX + 300 DOCX = 1800 files (too many!)
```
**YES:**
```
β
Extract text from all β Store in 1 Parquet file
```
---
## π COMMON GOVERNMENT FORMATS
| Format | Extension | Usage | Extraction Library |
|--------|-----------|-------|-------------------|
| **PDF** | .pdf | 70% - Most common | PyPDF2, pdfplumber, pypdf |
| **PowerPoint** | .ppt, .pptx | 15% - Presentations | python-pptx |
| **Word** | .doc, .docx | 10% - Agendas/Minutes | python-docx |
| **Excel** | .xls, .xlsx | 3% - Data tables | openpyxl, pandas |
| **HTML** | .html, .htm | 1% - Web pages | BeautifulSoup |
| **Images** | .jpg, .png | 1% - Scanned docs | pytesseract (OCR) |
**Solution: Handle ALL formats, extract text, store in same Parquet structure** β
---
## π§ INSTALLATION
```bash
# Install all document processing libraries
pip install PyPDF2 pdfplumber
pip install python-pptx
pip install python-docx
pip install openpyxl pandas
pip install beautifulsoup4 lxml
pip install pytesseract pillow # For OCR (scanned documents)
# Optional: Install Tesseract OCR engine
# Ubuntu/Debian:
sudo apt-get install tesseract-ocr
# macOS:
brew install tesseract
# Windows:
# Download from https://github.com/UB-Mannheim/tesseract/wiki
```
---
## π UNIVERSAL TEXT EXTRACTOR
### Complete Implementation:
```python
#!/usr/bin/env python3
"""
Universal document text extractor for government documents.
Handles: PDF, PPTX, DOCX, XLSX, HTML, Images (OCR)
"""
import io
from pathlib import Path
from typing import Optional, Dict
import httpx
from loguru import logger
# PDF extraction
try:
from PyPDF2 import PdfReader
import pdfplumber
except ImportError:
logger.warning("Install PDF tools: pip install PyPDF2 pdfplumber")
# PowerPoint extraction
try:
from pptx import Presentation
except ImportError:
logger.warning("Install PowerPoint tools: pip install python-pptx")
# Word extraction
try:
from docx import Document
except ImportError:
logger.warning("Install Word tools: pip install python-docx")
# Excel extraction
try:
import openpyxl
import pandas as pd
except ImportError:
logger.warning("Install Excel tools: pip install openpyxl pandas")
# HTML extraction
try:
from bs4 import BeautifulSoup
except ImportError:
logger.warning("Install HTML tools: pip install beautifulsoup4")
# OCR extraction (for images/scanned PDFs)
try:
import pytesseract
from PIL import Image
except ImportError:
logger.warning("Install OCR tools: pip install pytesseract pillow")
class UniversalDocumentExtractor:
"""Extract text from any government document format."""
def __init__(self):
self.client = httpx.Client(timeout=30)
def extract_from_url(self, url: str) -> Dict[str, any]:
"""
Download document from URL and extract text.
Args:
url: Document URL
Returns:
Dict with extracted text and metadata
"""
logger.info(f"Downloading: {url}")
# Download file
response = self.client.get(url)
file_bytes = response.content
# Detect format from URL or Content-Type
file_ext = self._detect_format(url, response.headers.get('content-type', ''))
# Extract based on format
if file_ext == '.pdf':
text = self.extract_pdf(file_bytes)
elif file_ext in ['.ppt', '.pptx']:
text = self.extract_powerpoint(file_bytes)
elif file_ext in ['.doc', '.docx']:
text = self.extract_word(file_bytes)
elif file_ext in ['.xls', '.xlsx']:
text = self.extract_excel(file_bytes)
elif file_ext in ['.html', '.htm']:
text = self.extract_html(file_bytes)
elif file_ext in ['.jpg', '.jpeg', '.png', '.tiff']:
text = self.extract_image_ocr(file_bytes)
else:
logger.warning(f"Unknown format: {file_ext}")
text = ""
return {
'url': url,
'format': file_ext,
'text': text,
'file_size_kb': len(file_bytes) // 1024,
'text_length': len(text)
}
def _detect_format(self, url: str, content_type: str) -> str:
"""Detect document format from URL or Content-Type."""
# Try URL extension first
url_lower = url.lower()
for ext in ['.pdf', '.pptx', '.ppt', '.docx', '.doc', '.xlsx', '.xls', '.html', '.htm', '.jpg', '.png']:
if ext in url_lower:
return ext
# Try Content-Type
content_type_lower = content_type.lower()
if 'pdf' in content_type_lower:
return '.pdf'
elif 'powerpoint' in content_type_lower or 'presentation' in content_type_lower:
return '.pptx'
elif 'word' in content_type_lower or 'msword' in content_type_lower:
return '.docx'
elif 'excel' in content_type_lower or 'spreadsheet' in content_type_lower:
return '.xlsx'
elif 'html' in content_type_lower:
return '.html'
return '.unknown'
def extract_pdf(self, file_bytes: bytes) -> str:
"""Extract text from PDF."""
try:
# Try PyPDF2 first (faster)
pdf_reader = PdfReader(io.BytesIO(file_bytes))
text = ""
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
# If no text extracted, might be scanned PDF
if not text.strip():
logger.info("PDF appears to be scanned, trying OCR...")
# Try pdfplumber or OCR
with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
text = "\n".join(page.extract_text() or "" for page in pdf.pages)
return text.strip()
except Exception as e:
logger.error(f"PDF extraction failed: {e}")
return ""
def extract_powerpoint(self, file_bytes: bytes) -> str:
"""Extract text from PowerPoint (.ppt, .pptx)."""
try:
prs = Presentation(io.BytesIO(file_bytes))
text_parts = []
for slide_num, slide in enumerate(prs.slides, 1):
# Extract text from all shapes
slide_text = []
for shape in slide.shapes:
if hasattr(shape, "text"):
slide_text.append(shape.text)
if slide_text:
text_parts.append(f"=== Slide {slide_num} ===\n")
text_parts.append("\n".join(slide_text))
text_parts.append("\n\n")
return "".join(text_parts).strip()
except Exception as e:
logger.error(f"PowerPoint extraction failed: {e}")
return ""
def extract_word(self, file_bytes: bytes) -> str:
"""Extract text from Word (.doc, .docx)."""
try:
doc = Document(io.BytesIO(file_bytes))
# Extract paragraphs
text_parts = []
for para in doc.paragraphs:
if para.text.strip():
text_parts.append(para.text)
# Extract tables
for table in doc.tables:
for row in table.rows:
row_text = " | ".join(cell.text for cell in row.cells)
if row_text.strip():
text_parts.append(row_text)
return "\n".join(text_parts).strip()
except Exception as e:
logger.error(f"Word extraction failed: {e}")
return ""
def extract_excel(self, file_bytes: bytes) -> str:
"""Extract text from Excel (.xls, .xlsx)."""
try:
# Use pandas to read all sheets
excel_file = io.BytesIO(file_bytes)
all_sheets = pd.read_excel(excel_file, sheet_name=None)
text_parts = []
for sheet_name, df in all_sheets.items():
text_parts.append(f"=== Sheet: {sheet_name} ===\n")
# Convert DataFrame to text
text_parts.append(df.to_string(index=False))
text_parts.append("\n\n")
return "".join(text_parts).strip()
except Exception as e:
logger.error(f"Excel extraction failed: {e}")
return ""
def extract_html(self, file_bytes: bytes) -> str:
"""Extract text from HTML."""
try:
soup = BeautifulSoup(file_bytes, 'html.parser')
# Remove script and style tags
for script in soup(["script", "style"]):
script.decompose()
# Get text
text = soup.get_text()
# Clean up whitespace
lines = (line.strip() for line in text.splitlines())
chunks = (phrase.strip() for line in lines for phrase in line.split(" "))
text = '\n'.join(chunk for chunk in chunks if chunk)
return text.strip()
except Exception as e:
logger.error(f"HTML extraction failed: {e}")
return ""
def extract_image_ocr(self, file_bytes: bytes) -> str:
"""Extract text from image using OCR (for scanned documents)."""
try:
image = Image.open(io.BytesIO(file_bytes))
# Run OCR
text = pytesseract.image_to_string(image)
return text.strip()
except Exception as e:
logger.error(f"OCR extraction failed: {e}")
logger.info("Make sure tesseract is installed: sudo apt-get install tesseract-ocr")
return ""
def close(self):
"""Close HTTP client."""
self.client.close()
# Example usage
if __name__ == "__main__":
extractor = UniversalDocumentExtractor()
# Test different formats
test_urls = [
"https://example.com/agenda.pdf",
"https://example.com/presentation.pptx",
"https://example.com/minutes.docx",
"https://example.com/budget.xlsx",
]
results = []
for url in test_urls:
try:
result = extractor.extract_from_url(url)
results.append(result)
print(f"β
{result['format']}: {result['text_length']} characters")
except Exception as e:
print(f"β Failed: {url} - {e}")
extractor.close()
# Save to Parquet
import pandas as pd
df = pd.DataFrame(results)
df.to_parquet('extracted_documents.parquet', compression='snappy')
print(f"\nβ
Saved {len(df)} documents to Parquet!")
```
---
## π PRACTICAL USAGE
### Process Mixed-Format Documents:
```python
import pandas as pd
from pathlib import Path
def process_jurisdiction_all_formats(jurisdiction):
"""
Process all document formats from a jurisdiction.
Extract text from PDFs, PPTX, DOCX, XLSX, etc.
Store all in single Parquet file.
"""
extractor = UniversalDocumentExtractor()
all_documents = []
# Get all document URLs (various formats)
document_urls = get_jurisdiction_documents(jurisdiction)
for url in document_urls:
# Extract text (works for any format!)
result = extractor.extract_from_url(url)
# Add metadata
all_documents.append({
'jurisdiction': jurisdiction.name,
'state': jurisdiction.state,
'url': result['url'],
'format': result['format'],
'text': result['text'],
'file_size_kb': result['file_size_kb'],
'date': extract_date_from_text(result['text']),
'title': extract_title_from_text(result['text'])
})
extractor.close()
# Save all formats in single Parquet
df = pd.DataFrame(all_documents)
df.to_parquet(f'documents_{jurisdiction.name}.parquet')
return df
# Process all jurisdictions
all_data = []
for jurisdiction in jurisdictions:
df = process_jurisdiction_all_formats(jurisdiction)
all_data.append(df)
# Combine all into one Parquet
combined = pd.concat(all_data, ignore_index=True)
combined.to_parquet('all_documents_all_formats.parquet', compression='snappy')
print(f"β
Processed {len(combined)} documents")
print(f" Formats: {combined['format'].value_counts().to_dict()}")
print(f" File size: {Path('all_documents_all_formats.parquet').stat().st_size / 1e6:.1f} MB")
```
---
## π REAL-WORLD EXAMPLE
### Tuscaloosa, AL (Mixed Formats):
```python
import asyncio
from universal_extractor import UniversalDocumentExtractor
async def discover_tuscaloosa_all_formats():
"""Find and process all document formats from Tuscaloosa."""
extractor = UniversalDocumentExtractor()
# Discover documents (various formats)
base_url = "https://tuscaloosaal.suiteonemedia.com"
# These might be PDFs, PPTX, DOCX, etc.
document_urls = [
f"{base_url}/agenda_2025_03_15.pdf",
f"{base_url}/presentation_budget.pptx",
f"{base_url}/minutes_2025_03_01.docx",
f"{base_url}/financial_report.xlsx",
]
results = []
for url in document_urls:
result = extractor.extract_from_url(url)
results.append(result)
print(f"Extracted {result['format']}: {result['text_length']} chars")
extractor.close()
# Save all in Parquet
import pandas as pd
df = pd.DataFrame(results)
df.to_parquet('tuscaloosa_all_formats.parquet')
print(f"\nβ
Saved {len(df)} documents (mixed formats) to 1 Parquet file")
print(f" Formats: {df['format'].value_counts().to_dict()}")
asyncio.run(discover_tuscaloosa_all_formats())
```
**Output:**
```
Extracted .pdf: 12,453 chars
Extracted .pptx: 3,821 chars
Extracted .docx: 8,234 chars
Extracted .xlsx: 1,562 chars
β
Saved 4 documents (mixed formats) to 1 Parquet file
Formats: {'.pdf': 1, '.pptx': 1, '.docx': 1, '.xlsx': 1}
```
---
## π― FORMAT-SPECIFIC TIPS
### PDF (70% of documents)
```python
# Use pdfplumber for better table extraction
import pdfplumber
with pdfplumber.open(pdf_file) as pdf:
# Extract text + tables
for page in pdf.pages:
text = page.extract_text()
tables = page.extract_tables() # Get structured tables!
```
### PowerPoint (15% of documents)
```python
# Extract speaker notes too
from pptx import Presentation
prs = Presentation(pptx_file)
for slide in prs.slides:
# Text from shapes
for shape in slide.shapes:
if hasattr(shape, "text"):
print(shape.text)
# Speaker notes
if slide.has_notes_slide:
print(slide.notes_slide.notes_text_frame.text)
```
### Word (10% of documents)
```python
# Extract headers, footers, comments
from docx import Document
doc = Document(docx_file)
# Headers/Footers
for section in doc.sections:
print(section.header.paragraphs[0].text)
print(section.footer.paragraphs[0].text)
# Comments (track changes)
for comment in doc.comments:
print(comment.text)
```
### Excel (3% of documents)
```python
# Extract all sheets + formulas
import pandas as pd
# Read all sheets
excel_data = pd.read_excel(xlsx_file, sheet_name=None)
for sheet_name, df in excel_data.items():
print(f"Sheet: {sheet_name}")
print(df.to_string())
```
---
## πΎ FINAL PARQUET STRUCTURE
**Regardless of input format, output is unified:**
```python
# Single Parquet file with all formats
df = pd.DataFrame({
'jurisdiction': ['Tuscaloosa', 'Tuscaloosa', 'Tuscaloosa'],
'state': ['AL', 'AL', 'AL'],
'date': ['2025-03-15', '2025-03-15', '2025-03-01'],
'title': ['City Council Meeting', 'Budget Presentation', 'Meeting Minutes'],
'format': ['.pdf', '.pptx', '.docx'], # β Track original format
'text': ['extracted text...', 'slide text...', 'minutes text...'],
'url': ['https://...agenda.pdf', 'https://...budget.pptx', 'https://...minutes.docx']
})
# Save to Parquet
df.to_parquet('all_formats.parquet', compression='snappy')
# Upload to Hugging Face (1 file, not 3!)
from datasets import Dataset
dataset = Dataset.from_pandas(df)
dataset.push_to_hub("username/oral-health-docs")
```
---
## π HANDLING SPECIAL CASES
### Scanned PDFs (Images)
```python
# Use OCR for scanned documents
import pytesseract
import pdf2image
# Convert PDF pages to images, then OCR
images = pdf2image.convert_from_bytes(pdf_bytes)
text = ""
for img in images:
text += pytesseract.image_to_string(img) + "\n"
```
### Password-Protected PDFs
```python
# Some government docs are password-protected
from PyPDF2 import PdfReader
reader = PdfReader(pdf_file)
if reader.is_encrypted:
# Try common passwords
passwords = ['', 'password', 'public']
for pwd in passwords:
if reader.decrypt(pwd):
break
```
### Embedded Videos/Audio
```python
# Don't extract video/audio files
# Just note their existence and link to them
if 'video' in doc.format or 'audio' in doc.format:
return {
'text': '[Video/Audio content - see URL]',
'url': doc_url,
'type': 'multimedia'
}
```
---
## β
SUMMARY
### Key Points:
1. **Government sites use many formats**
- PDF (70%), PowerPoint (15%), Word (10%), Excel (3%), Others (2%)
2. **Solution: Universal extractor**
- One tool handles all formats
- Extract text from everything
- Store in single Parquet file
3. **Same workflow regardless of format**
```
Download β Extract Text β Store in Parquet β Upload to HF
```
4. **File limits still respected**
- 1,000 PDFs + 500 PPTX + 300 DOCX = 1,800 source files
- Extract β Save as 1 Parquet file β
5. **Hugging Face upload**
- Upload Parquet (not source files)
- All formats in unified structure
- Still FREE unlimited storage
### Libraries Needed:
```bash
pip install PyPDF2 pdfplumber # PDF
pip install python-pptx # PowerPoint
pip install python-docx # Word
pip install openpyxl pandas # Excel
pip install beautifulsoup4 # HTML
pip install pytesseract pillow # OCR for scanned docs
```
### Result:
**You can now handle ANY format government sites use, extract text, and store efficiently in Parquet for FREE on Hugging Face!** π
---
**Next:** Integrate this into your discovery pipeline so it automatically handles all formats!
|