ogx786 commited on
Commit
2c1318d
·
verified ·
1 Parent(s): 2182983

Create spliter.py

Browse files
Files changed (1) hide show
  1. spliter.py +218 -0
spliter.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ PDF Text/Image Classifier
3
+ --------------------------
4
+
5
+ Separates PDFs into:
6
+
7
+ text_pdfs/
8
+ PDFs with extractable text
9
+
10
+ image_pdfs/
11
+ Scanned/image-only PDFs requiring OCR
12
+
13
+
14
+ Usage:
15
+
16
+ python split_pdf_types.py
17
+
18
+ """
19
+
20
+ from pathlib import Path
21
+ import shutil
22
+
23
+ import fitz
24
+ from tqdm import tqdm
25
+
26
+
27
+ # -----------------------------
28
+ # CONFIG
29
+ # -----------------------------
30
+
31
+ INPUT_FOLDER = Path("./pdfs")
32
+
33
+ OUTPUT_FOLDER = Path("./classified_pdfs")
34
+
35
+ TEXT_FOLDER = OUTPUT_FOLDER / "text_pdfs"
36
+
37
+ IMAGE_FOLDER = OUTPUT_FOLDER / "image_pdfs"
38
+
39
+
40
+ # Minimum characters per page
41
+ # below this -> considered scanned
42
+
43
+ MIN_CHARS_PER_PAGE = 30
44
+
45
+
46
+
47
+ # -----------------------------
48
+ # CLASSIFICATION
49
+ # -----------------------------
50
+
51
+ def classify_pdf(pdf_path):
52
+
53
+ """
54
+ Returns:
55
+
56
+ TEXT
57
+ IMAGE
58
+
59
+ """
60
+
61
+ try:
62
+
63
+ doc = fitz.open(pdf_path)
64
+
65
+
66
+ total_chars = 0
67
+ total_pages = len(doc)
68
+
69
+ image_pages = 0
70
+
71
+
72
+ for page in doc:
73
+
74
+
75
+ text = page.get_text().strip()
76
+
77
+
78
+ total_chars += len(text)
79
+
80
+
81
+
82
+ # Count pages containing images
83
+
84
+ if len(page.get_images(full=True)) > 0:
85
+ image_pages += 1
86
+
87
+
88
+
89
+ avg_chars = total_chars / max(total_pages,1)
90
+
91
+
92
+
93
+ #
94
+ # Rules
95
+ #
96
+
97
+ # No text layer
98
+ if avg_chars < MIN_CHARS_PER_PAGE:
99
+
100
+ return "IMAGE"
101
+
102
+
103
+
104
+ # Mostly image pages + little text
105
+ if image_pages / total_pages > 0.7 and avg_chars < 100:
106
+
107
+ return "IMAGE"
108
+
109
+
110
+
111
+ return "TEXT"
112
+
113
+
114
+
115
+ except Exception as e:
116
+
117
+ print(
118
+ f"Error reading {pdf_path.name}: {e}"
119
+ )
120
+
121
+ return "IMAGE"
122
+
123
+
124
+
125
+
126
+
127
+ # -----------------------------
128
+ # MAIN
129
+ # -----------------------------
130
+
131
+ def main():
132
+
133
+
134
+ TEXT_FOLDER.mkdir(
135
+ parents=True,
136
+ exist_ok=True
137
+ )
138
+
139
+
140
+ IMAGE_FOLDER.mkdir(
141
+ parents=True,
142
+ exist_ok=True
143
+ )
144
+
145
+
146
+ pdfs=list(
147
+ INPUT_FOLDER.glob("*.pdf")
148
+ )
149
+
150
+
151
+ print(
152
+ f"Found {len(pdfs)} PDFs"
153
+ )
154
+
155
+
156
+ stats={
157
+ "TEXT":0,
158
+ "IMAGE":0
159
+ }
160
+
161
+
162
+
163
+ for pdf in tqdm(pdfs):
164
+
165
+
166
+ category = classify_pdf(pdf)
167
+
168
+
169
+ if category=="TEXT":
170
+
171
+ destination = (
172
+ TEXT_FOLDER /
173
+ pdf.name
174
+ )
175
+
176
+
177
+ else:
178
+
179
+ destination = (
180
+ IMAGE_FOLDER /
181
+ pdf.name
182
+ )
183
+
184
+
185
+
186
+ shutil.copy2(
187
+ pdf,
188
+ destination
189
+ )
190
+
191
+
192
+ stats[category]+=1
193
+
194
+
195
+
196
+
197
+ print("\nCompleted")
198
+
199
+ print(
200
+ f"Text PDFs : {stats['TEXT']}"
201
+ )
202
+
203
+ print(
204
+ f"Image PDFs : {stats['IMAGE']}"
205
+ )
206
+
207
+ print("\nOutput:")
208
+ print(
209
+ TEXT_FOLDER
210
+ )
211
+ print(
212
+ IMAGE_FOLDER
213
+ )
214
+
215
+
216
+
217
+ if __name__=="__main__":
218
+ main()