Add scripts
This commit is contained in:
commit
cc682741d5
6 changed files with 437 additions and 0 deletions
69
extract_layers.py
Normal file
69
extract_layers.py
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#!/usr/bin/env python3
|
||||
"""
|
||||
analyze Optional Content Groups (Layers) in PDFs recursively
|
||||
"""
|
||||
import fitz
|
||||
from pathlib import Path
|
||||
|
||||
def analyze_ocg(pdf_path):
|
||||
try:
|
||||
doc = fitz.open(pdf_path)
|
||||
ocgs = []
|
||||
|
||||
if doc.is_pdf and hasattr(doc, 'get_ocgs'):
|
||||
ocg_list = doc.get_ocgs()
|
||||
if ocg_list:
|
||||
for ocg in ocg_list:
|
||||
ocgs.append({
|
||||
'name': ocg.get('name', 'Unknown'),
|
||||
'on': ocg.get('on', None),
|
||||
'intent': ocg.get('intent', []),
|
||||
'usage': ocg.get('usage', {})
|
||||
})
|
||||
|
||||
doc.close()
|
||||
|
||||
if ocgs:
|
||||
return {
|
||||
'file': str(pdf_path),
|
||||
'ocg_count': len(ocgs),
|
||||
'layers': ocgs
|
||||
}
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
return {
|
||||
'file': str(pdf_path),
|
||||
'error': str(e)
|
||||
}
|
||||
|
||||
def main():
|
||||
pdf_files = Path('.').rglob('*.pdf')
|
||||
found_count = 0
|
||||
|
||||
print(f"{'SCANNING FOR LAYERS':=^60}")
|
||||
|
||||
for pdf_file in pdf_files:
|
||||
result = analyze_ocg(pdf_file)
|
||||
|
||||
if result:
|
||||
found_count += 1
|
||||
print(f"\nFile: {result['file']}")
|
||||
|
||||
if 'error' in result:
|
||||
print(f" [!] Error: {result['error']}")
|
||||
print("-" * 60)
|
||||
continue
|
||||
|
||||
print(f" Layer Count: {result['ocg_count']}")
|
||||
print(" Layers:")
|
||||
|
||||
for layer in result['layers']:
|
||||
state = "ON " if layer['on'] else "OFF"
|
||||
print(f" [{state}] {layer['name']}")
|
||||
print("-" * 60)
|
||||
|
||||
print(f"\nAnalysis complete. Found {found_count} PDFs with layers or errors.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue