-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpdf2markdown.py
More file actions
211 lines (155 loc) · 7.73 KB
/
Copy pathpdf2markdown.py
File metadata and controls
211 lines (155 loc) · 7.73 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
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
import pdftotext
COLUMN_WIDTH = 64
class Page:
def __init__(self, page_string, skip = 'skip this line', bulletpoint_format = True, bullet = '', merge_lines = True, width = COLUMN_WIDTH):
'''
skip: skip line in output if page contains string, e.g., ' / 10' as page label
'''
self.page_string = page_string
self.lines = self.page_string.replace(' ','').split('\n')
self.parsed = self.lines
self.skip = skip
self.bulletpoint_format = bulletpoint_format
self.bullet = bullet
self.merge_lines = merge_lines
self.width = width
def makeTitle(self):
self.title = '\n##### ' + self.lines[0]
temp = self.lines
temp[0] = self.title
self.parsed = temp
return self
def parsePage(self):
string = ''
for line in self.parsed:
not_skipped = True
new_line = True
is_list_item = False
if line == '':
not_skipped = False
if type(self.skip) == list:
for skipping_item in self.skip:
if skipping_item in line:
not_skipped = False
else:
if self.skip in line:
not_skipped = False
if not_skipped == True:
curr_line = Line(line.replace('↵',''), self.bullet)
temp = line
if self.bulletpoint_format == True:
try:
if (temp.lstrip(' ')[0] == self.bullet) or (temp.lstrip(' ')[1] == self.bullet) or (temp.lstrip(' ')[1] == '.') :
is_list_item = True
curr_line.makeListItem()
elif (len(line) < self.width) and (self.merge_lines is True):
new_line = False
except IndexError:
if (len(line) < self.width) and (self.merge_lines is True):
new_line = False
curr_line = curr_line.parsed
if new_line == True:
string += '\n' + curr_line + ' '
else:
string += curr_line + ' '
else:
string += '\n' + line + ' '
self.parsed = string
return self
class Line:
def __init__(self, line_string, bullet):
self.line_string = line_string
self.parsed = line_string
self.bullet = bullet
def makeListItem(self):
self.parsed = self.parsed.replace(self.bullet, '- ')
return self
class PDF:
def __init__(self, file, pw='', skip='skip this line', manual=False, bulletpoint_format=True, outfile='output.txt', bullet='- ', merge_lines=False, width = COLUMN_WIDTH):
self.file = file
self.pw = pw
with open(file, "rb") as f:
if self.pw != '':
pdf = pdftotext.PDF(f, self.pw)
else:
pdf = pdftotext.PDF(f)
self.pdf = pdf
self.n_pages = len(pdf)
self.manual = manual
self.bulletpoint_format = bulletpoint_format
self.skip = skip
self.outfile = outfile
self.bullet = bullet
self.merge_lines = merge_lines
self.width = width
def parsePDF(self):
if self.manual is False:
with open(outfile, 'w') as output:
for page in self.pdf:
output.write('\n')
cur_page = Page(page, self.skip, bulletpoint_format=self.bulletpoint_format, bullet=self.bullet, merge_lines=self.merge_lines, width = self.width)
cur_page = cur_page.makeTitle()
cur_page = cur_page.parsePage()
print(cur_page.parsed)
for parsed_line in cur_page.parsed:
output.write(parsed_line)
else:
for i, page in enumerate(self.pdf):
with open(outfile, 'w') as output:
satisfied = False
while not satisfied:
print('\n------------processing page %i / %i------------\n'%(i+1, self.n_pages))
skip = str(input('skip lines containing the following (separate multiple with ";"), default "skip this line": ') or 'skip this line')
skip = skip.split(';')
merge_lines = input('merge broken lines? (T: yes -> form paragraphs, default / F: no, leave as they exist in the pdf): ') or 'T'
if merge_lines == 'F':
merge_lines = False
else:
merge_lines = True
bulletpoint_format = input('modify format (T: bulletpoint_format, default / F: original document): ') or 'T'
if bulletpoint_format == 'T':
bulletpoint_format = True
bullet = str(input('identifier for bullet point: ') or '- ')
else:
bulletpoint_format = False
bullet = '- '
width = int(input('merge lines shorter than (max no. of characters): ') or 64)
cur_page = Page(page, skip, bulletpoint_format, bullet, merge_lines, width)
cur_page = cur_page.makeTitle()
cur_page = cur_page.parsePage()
print('\n ------------OUTPUT------------')
print(cur_page.parsed)
print('\n--------------------------------\n')
ok = input('satisfied? (y/n):')
if ok == 'y':
output.write('\n')
for parsed_line in cur_page.parsed:
output.write(parsed_line)
print('ok, start with the next page!')
satisfied = True
else:
print('\nRedo current page.')
if __name__ == "__main__":
filename = input('filename: ')
pw = str(input('password (if any): ') or '')
manual = 0
outfile = str(input('save as (default output.txt)') or 'output.txt')
manual = input('go through document page by page with individual parameters (T: yes / F: no, default): ') or 'F'
if manual == 'T':
PDF(filename, pw, 'skip this line', True).parsePDF()
elif manual == 'F':
width = int(input('merge lines shorter than (max no. of characters): ') or COLUMN_WIDTH)
skip = str(input('skip lines containing the following (separate multiple with ";"), default "skip this line": ') or 'skip this line')
skip = skip.split(';')
merge_lines = input('merge broken lines? (T: yes -> form paragraphs, default / F: no, leave as they exist in the pdf): ') or 'T'
if merge_lines == 'F':
merge_lines = False
else:
merge_lines = True
bulletpoint_format = input('modify format (T: bulletpoint_format, default / F: original document): ') or 'T'
if bulletpoint_format == 'T':
bulletpoint_format = True
bullet = str(input('identifier for bullet point: ') or '- ')
else:
bulletpoint_format = False
PDF(filename, pw, skip, False, bulletpoint_format, outfile, bullet, merge_lines, width).parsePDF()