forked from nathfavour/bionicpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbionicpython.py
More file actions
393 lines (270 loc) · 11.4 KB
/
bionicpython.py
File metadata and controls
393 lines (270 loc) · 11.4 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import argparse
import math
from docx import Document
import os
import argparse
import math
from docx import Document
import os
import spacy
import subprocess
import sys
import re
def process_word(word, ratio):
# Calculate the number of characters to make bold
num_chars = math.ceil(len(word) * ratio)
# Split the word into the part that should be bold and the part that shouldn't
bold_part = word[:num_chars]
normal_part = word[num_chars:]
# Create runs for the bold part and the normal part
bold_run = (bold_part, True)
normal_run = (normal_part, False)
# Return the runs as a list
return [bold_run, normal_run]
def process_document(doc_path, ratio):
output_path = os.path.splitext(doc_path)[0] + '_modified.docx'
# Check if the file path is for a .docx file
if doc_path.endswith('.docx'):
print("Already in docx format")
elif doc_path.endswith('.pdf'):
docx_path = doc_path.replace('.pdf', '.docx')
try:
# Run the conversion script as a subprocess
subprocess.run([sys.executable, 'converter.py', '--pdf', doc_path, '--docx', docx_path])
# Replace '.pdf' with '.docx' in the file path
doc_path = docx_path
except subprocess.CalledProcessError as e:
print("Error converting PDF:", e)
sys.exit(1)
else:
print("Files of this format are not supported yet")
print("Please use either .pdf or .docx files")
sys.exit()
# Load the spacy model for word recognition (wrap in try-except)
try:
nlp = spacy.load('en_core_web_sm')
except OSError as e:
print("Error loading spaCy model:", e)
sys.exit(1)
try:
# Open the .docx file
word_doc = Document(doc_path)
for paragraph in word_doc.paragraphs:
for run in paragraph.runs:
# Skip if the run is already bold
if run.bold:
continue
# Split the run text into words
words = run.text.split(' ')
words = [' ' + word if i != 0 else word for i, word in enumerate(words)]
# Process each word
new_runs = []
for word in words:
# Use spacy to recognize the words
doc = nlp(word)
for token in doc:
# Bolden a ratio of the characters in the word
runs = process_word(token.text, ratio)
new_runs.extend(runs)
# Clear the original run
run.text = ''
# Add new runs with the appropriate formatting
for text, is_bold in new_runs:
new_run = paragraph.add_run(text)
new_run.bold = is_bold
# Save the document (wrap in try-except)
try:
word_doc.save(output_path)
except PermissionError as e:
print("Error saving document:", e)
sys.exit(1)
except Exception as e: # Catch any other unexpected errors
print("Unexpected error processing document:", e)
sys.exit(1)
# Get the directory and filename from the input path
dir_name, file_name = os.path.split(doc_path)
# Prepend 'processed_' to the filename
new_file_name = 'processed_' + file_name
# Combine the directory and new filename to get the output path
# output_path = os.path.splitext(doc_path)[0] + '_modified.docx'
print(output_path)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('doc_path', help='Path to the .docx file')
parser.add_argument('--ratio', type=float, default=0.5, help='Ratio of characters to make bold')
args = parser.parse_args()
process_document(args.doc_path, args.ratio)
if __name__ == '__main__':
main()
# def process_document(doc_path, ratio):
# # Check if the file path is for a .docx file
# if not doc_path.endswith('.docx'):
# print("Invalid file format. Please provide a .docx file.")
# return
# # Load the spacy model for word recognition
# nlp = spacy.load('en_core_web_sm')
# # Open the .docx file
# doc = Document(doc_path)
# for paragraph in doc.paragraphs:
# for run in paragraph.runs:
# # Skip if the run is already bold
# if run.bold:
# continue
# # Split the run text into words
# words = run.text.split()
# # Process each word
# new_runs = []
# for word in words:
# # Use spacy to recognize the words
# doc = nlp(word)
# for token in doc:
# # Bolden a ratio of the characters in the word
# runs = process_word(token.text, ratio)
# new_runs.extend(runs)
# # Clear the original run
# run.text = ''
# # Add new runs with the appropriate formatting
# for text, is_bold in new_runs:
# new_run = paragraph.add_run(text)
# new_run.bold = is_bold
# # Get the directory and filename from the input path
# dir_name, file_name = os.path.split(doc_path)
# # Prepend 'processed_' to the filename
# new_file_name = 'processed_' + file_name
# # Combine the directory and new filename to get the output path
# output_path = os.path.join(dir_name, new_file_name)
# doc.save(output_path)
# def process_word(word, ratio):
# # Calculate the number of characters to make bold
# num_chars = math.ceil(len(word) * ratio)
# # Make the specified number of characters bold
# bold_word = word[:num_chars] + word[num_chars:].bold()
# return bold_word
# def process_document(doc_path, ratio):
# # Check if the file path is for a .docx file
# if not doc_path.endswith('.docx'):
# print("Invalid file format. Please provide a .docx file.")
# return
# # Load the spacy model for word recognition
# nlp = spacy.load('en_core_web_sm')
# # Open the .docx file
# doc = Document(doc_path)
# for paragraph in doc.paragraphs:
# for run in paragraph.runs:
# # Skip if the run is already bold
# if run.bold:
# continue
# # Split the run text into words
# words = run.text.split()
# # Process each word
# new_words = []
# for word in words:
# # Use spacy to recognize the words
# doc = nlp(word)
# for token in doc:
# # Bolden a ratio of the characters in the word
# bold_word = process_word(token.text, ratio)
# new_words.append(bold_word)
# # Replace the run text with the processed words
# run.text = ' '.join(new_words)
# print("processed another paragraph...")
# # Get the directory and filename from the input path
# dir_name, file_name = os.path.split(doc_path)
# # Prepend 'processed_' to the filename
# new_file_name = 'processed_' + file_name
# # Combine the directory and new filename to get the output path
# output_path = os.path.join(dir_name, new_file_name)
# doc.save(output_path)
# def main():
# parser = argparse.ArgumentParser()
# parser.add_argument('doc_path', help='Path to the .docx file')
# parser.add_argument('--ratio', type=float, default=0.5, help='Ratio of characters to make bold')
# args = parser.parse_args()
# process_document(args.doc_path, args.ratio)
# if __name__ == '__main__':
# main()
# python bionic.py /path/to/your/document.docx
# def process_word(word, ratio):
# # Calculate the number of characters to make bold
# num_bold_chars = math.ceil(len(word) * ratio)
# # Create a new run for each part of the word
# bold_run = word[:num_bold_chars]
# normal_run = word[num_bold_chars:]
# return bold_run, normal_run
# # def process_document(doc_path, ratio):
# # doc = Document(doc_path)
# # for paragraph in doc.paragraphs:
# # for run in paragraph.runs:
# # # Skip if the run is already bold
# # if run.bold:
# # continue
# # # Split the run text into words
# # words = run.text.split()
# # # Process each word
# # new_words = []
# # for word in words:
# # bold_run, normal_run = process_word(word, ratio)
# # new_words.append(bold_run)
# # new_words.append(normal_run)
# # # Replace the run text with the processed words
# # run.text = ' '.join(new_words)
# # doc.save('processed_' + doc_path)
# # def process_document(doc_path, ratio):
# # doc = Document(doc_path)
# # for paragraph in doc.paragraphs:
# # for run in paragraph.runs:
# # # Skip if the run is already bold
# # if run.bold:
# # continue
# # # Split the run text into words
# # words = run.text.split()
# # # Process each word
# # new_words = []
# # for word in words:
# # bold_run, normal_run = process_word(word, ratio)
# # new_words.append(bold_run)
# # new_words.append(normal_run)
# # # Replace the run text with the processed words
# # run.text = ' '.join(new_words)
# # # Get the directory and filename from the input path
# # dir_name, file_name = os.path.split(doc_path)
# # # Prepend 'processed_' to the filename
# # new_file_name = 'processed_' + file_name
# # # Combine the directory and new filename to get the output path
# # output_path = os.path.join(dir_name, new_file_name)
# # doc.save(output_path)
# def process_document(doc_path, ratio):
# doc = Document(doc_path)
# new_doc = Document()
# for paragraph in doc.paragraphs:
# new_paragraph = new_doc.add_paragraph()
# for run in paragraph.runs:
# # Skip if the run is already bold
# if run.bold:
# continue
# # Split the run text into words
# words = run.text.split()
# # Process each word
# for word in words:
# bold_run_text, normal_run_text = process_word(word, ratio)
# # Add new runs to the new paragraph
# if bold_run_text:
# bold_run = new_paragraph.add_run(bold_run_text)
# bold_run.bold = True
# if normal_run_text:
# normal_run = new_paragraph.add_run(normal_run_text)
# # Get the directory and filename from the input path
# dir_name, file_name = os.path.split(doc_path)
# # Prepend 'processed_' to the filename
# new_file_name = 'processed_' + file_name
# # Combine the directory and new filename to get the output path
# output_path = os.path.join(dir_name, new_file_name)
# new_doc.save(output_path)
# def main():
# parser = argparse.ArgumentParser()
# parser.add_argument('doc_path', help='Path to the .docx file')
# parser.add_argument('--ratio', type=float, default=0.5, help='Ratio of characters to make bold')
# args = parser.parse_args()
# process_document(args.doc_path, args.ratio)
# if __name__ == '__main__':
# main()