forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf_extraction.py
150 lines (129 loc) · 4 KB
/
pdf_extraction.py
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
from pdfid import pdfid
import fitz
from os.path import exists
import sys
# Function to convert bytes to kilobytes
def bytes_to_kb(bytes):
return bytes / 1024
# Main function to extract features from the PDF file
def extract_pdf_features(pdf_file):
# Checking if the file exists
if not exists(pdf_file):
print(f"File {pdf_file} not found")
return None
features = {'FileName': pdf_file}
# Open the PDF
pdf = fitz.open(pdf_file)
# Extract basic PDF metadata using PyMuPDF
try:
features['Pages'] = pdf.page_count
except:
features['Pages'] = -1
try:
features['XrefLength'] = pdf.xref_length()
except:
features['XrefLength'] = -1
try:
features['TitleCharacters'] = len(pdf.metadata.get('title', ''))
except:
features['TitleCharacters'] = -1
features['isEncrypted'] = 1 if pdf.is_encrypted else 0
# Extract image-related features
images_count = 0
for i in range(pdf.page_count):
images_count += len(pdf.get_page_images(i))
features['Images'] = images_count
# Extract embedded file details
emb_count = pdf.embfile_count()
emb_size_sum = 0
if emb_count != 0:
try:
for i in range(emb_count):
emb_size_sum += pdf.embfile_info(i)
except:
features['EmbeddedFiles'] = -1
else:
features['EmbeddedFiles'] = emb_size_sum / emb_count
else:
features['EmbeddedFiles'] = 0
# Extract presence of text in the PDF
text = 0
for page in pdf:
if len(page.get_text().split()):
text = 1
break
features['Text'] = text
# Close the PDF after processing
pdf.close()
# Extract additional PDF features using pdfid
try:
options = pdfid.get_fake_options()
options.scan = True
options.json = True
list_of_dict = pdfid.PDFiDMain([pdf_file], options)
pdf_features = list_of_dict['reports'][0]
del pdf_features['version']
# Rename features to correspond to dataset names
diff_in_feature_name = {
'header': 'Header',
'obj': 'Obj',
'endobj': 'Endobj',
'stream': 'Stream',
'endstream': 'Endstream',
'xref': 'Xref',
'trailer': 'Trailer',
'startxref': 'StartXref',
'/Page': 'PageNo',
'/Encrypt': 'Encrypt',
'/ObjStm': 'ObjStm',
'/JS': 'JS',
'/JavaScript': 'JavaScript',
'/AA': 'AA',
'/OpenAction': 'OpenAction',
'/AcroForm': 'AcroForm',
'/JBIG2Decode': 'JBIG2Decode',
'/RichMedia': 'RichMedia',
'/Launch': 'Launch',
'/EmbeddedFile': 'EmbeddedFile',
'/XFA': 'XFA',
'/Colors > 2^24': 'Colors'
}
for curr_name, new_name in diff_in_feature_name.items():
pdf_features[new_name] = features.pop(curr_name, -1)
features.update(pdf_features)
except Exception as e:
print(f"Error extracting pdfid features: {e}")
features.update({
'Header': '-1',
'Obj': -1,
'Endobj': -1,
'Stream': -1,
'Endstream': -1,
'Xref': -1,
'Trailer': -1,
'StartXref': -1,
'PageNo': -1,
'Encrypt': -1,
'ObjStm': -1,
'JS': -1,
'JavaScript': -1,
'AA': -1,
'OpenAction': -1,
'AcroForm': -1,
'JBIG2Decode': -1,
'RichMedia': -1,
'Launch': -1,
'EmbeddedFile': -1,
'XFA': -1,
'Colors': -1
})
return features
# If run as a script
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python pdf_feature_extraction.py <pdf_file>")
sys.exit(1)
pdf_file = sys.argv[1]
features = extract_pdf_features(pdf_file)
if features:
print(features)