-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_documents.py
More file actions
179 lines (144 loc) · 4.8 KB
/
Copy pathscan_documents.py
File metadata and controls
179 lines (144 loc) · 4.8 KB
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
from __future__ import annotations
import json
import os
import subprocess
import tempfile
from datetime import datetime
from pathlib import Path
from typing import Any
try:
from pypdf import PdfReader
except ImportError:
PdfReader = None
try:
from docx import Document
except ImportError:
Document = None
PROJECT_ROOT = Path(__file__).resolve().parent
DATA_DIR = PROJECT_ROOT / "enterprise-data"
OUTPUT_DIR = PROJECT_ROOT / "output"
METADATA_FILE = DATA_DIR / "metadata.json"
BASE_DIR = DATA_DIR
OUTPUT_JSON = OUTPUT_DIR / "document_inventory.json"
SUPPORTED_EXTENSIONS = [".pdf", ".docx", ".txt", ".html", ".htm"]
def format_size(size_bytes: int) -> str:
if size_bytes < 1024:
return f"{size_bytes} B"
if size_bytes < 1024 * 1024:
return f"{size_bytes / 1024:.2f} KB"
return f"{size_bytes / (1024 * 1024):.2f} MB"
def format_date(timestamp: float) -> str:
return datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
def get_pdf_pages(file_path: Path) -> int | str:
if PdfReader is None:
return "Install pypdf"
try:
reader = PdfReader(str(file_path))
return len(reader.pages)
except Exception as exc:
return f"Error: {exc}"
def get_docx_pages(file_path: Path) -> int | str:
"""
DOCX does not store a reliable page count by default.
This function first tries LibreOffice conversion to PDF and counts PDF pages.
If LibreOffice is unavailable, it returns paragraph count as a fallback note.
"""
try:
with tempfile.TemporaryDirectory() as tmpdir:
result = subprocess.run(
[
"soffice",
"--headless",
"--convert-to",
"pdf",
"--outdir",
tmpdir,
str(file_path),
],
capture_output=True,
text=True,
timeout=30,
)
converted_pdf = Path(tmpdir) / f"{file_path.stem}.pdf"
if result.returncode == 0 and converted_pdf.exists():
return get_pdf_pages(converted_pdf)
except Exception:
pass
if Document is None:
return "Install python-docx"
try:
doc = Document(str(file_path))
return f"N/A ({len(doc.paragraphs)} paragraphs)"
except Exception as exc:
return f"Error: {exc}"
def get_pages(file_path: Path) -> int | str:
if file_path.suffix.lower() == ".pdf":
return get_pdf_pages(file_path)
if file_path.suffix.lower() == ".docx":
return get_docx_pages(file_path)
return "N/A"
def scan_documents() -> list[dict[str, Any]]:
inventory = []
for file_path in sorted(BASE_DIR.rglob("*")):
if not file_path.is_file():
continue
extension = file_path.suffix.lower()
if extension not in SUPPORTED_EXTENSIONS:
continue
stat = file_path.stat()
department = file_path.parent.name
inventory.append(
{
"document_name": file_path.name,
"type": extension.replace(".", "").upper(),
"size": format_size(stat.st_size),
"size_bytes": stat.st_size,
"pages": get_pages(file_path),
"department": department,
"created_date": format_date(stat.st_ctime),
"modified_date": format_date(stat.st_mtime),
"relative_path": str(file_path.relative_to(BASE_DIR.parent)),
}
)
return inventory
def print_table(rows: list[dict[str, Any]]) -> None:
headers = [
"Document Name",
"Type",
"Size",
"Pages",
"Department",
"Created Date",
"Modified Date",
]
table_rows = [
[
row["document_name"],
row["type"],
row["size"],
str(row["pages"]),
row["department"],
row["created_date"],
row["modified_date"],
]
for row in rows
]
widths = [len(header) for header in headers]
for row in table_rows:
for idx, value in enumerate(row):
widths[idx] = max(widths[idx], len(value))
def line(values: list[str]) -> str:
return " | ".join(value.ljust(widths[idx]) for idx, value in enumerate(values))
print(line(headers))
print("-+-".join("-" * width for width in widths))
for row in table_rows:
print(line(row))
def main() -> None:
if not BASE_DIR.exists():
raise FileNotFoundError(f"Folder not found: {BASE_DIR}")
inventory = scan_documents()
print_table(inventory)
OUTPUT_JSON.write_text(json.dumps(inventory, indent=2), encoding="utf-8")
print(f"\nInventory saved to: {OUTPUT_JSON}")
if __name__ == "__main__":
main()