-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_renamer.py
220 lines (165 loc) · 6.01 KB
/
python_renamer.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
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
import math
import os
# assign directory
directory = './example_papers'
# find
from PyPDF2 import PdfReader
# pip install PyMuPDF
import fitz
# import libraries
import fitz
import io
from PIL import Image
def some_magic(pdf_file_path):
'''
@param pdf_file_path:
@return:
'''
import images
for i in dir(images):
item = getattr(images, i)
if callable(item):
item(pdf_file_path)
os.system(f"mkdir to_delete")
for filepath_obj in os.scandir(directory):
if filepath_obj.is_file() and ".pdf" in filepath_obj.path:
print(filepath_obj.path)
try:
doc = PdfReader(open(filepath_obj.path, "rb"))
except:
print(filepath_obj.path +" is not a readable pdf file")
continue
else:
continue
import re
#if len(re.findall(".pdf", filepath_obj.path))>1:
# continue
doc = fitz.open(filepath_obj.path)
#if doc.metadata["producer"]== "paper_sorter":
# continue
doc.close()
'''
************************** https://stackoverflow.com/a/34431716/5550255 ********************
'''
# capture the output of pdftitle to get the article title
import subprocess
try:
captured_title = (subprocess.check_output(f"pdftitle -p '{filepath_obj.path}' -a max2 --replace-missing-char=# ",shell=True)) # the original pdf would be replaced with the one with new name
except subprocess.CalledProcessError as err:
print(err)
try:
captured_title = (
subprocess.check_output(f"pdftitle -p '{filepath_obj.path}' -a eliot --eliot-tfs=0 --replace-missing-char=# ",
shell=True)) # the original pdf would be replaced with the one with new name
except subprocess.CalledProcessError as err:
print(err)
continue
# Path
import os
captured_title = captured_title.decode()
captured_title = captured_title.strip()
if len(captured_title)<=5:
continue
# to remedy problem of "OSError File name too long", handle here rather than using pdftitle
import string
new_name = captured_title.lower() # Lower case name
valid_chars = set(string.ascii_lowercase + string.digits + " ")
new_name = "".join(c for c in new_name if c in valid_chars)
# split by space
words = str(new_name).split(" ")
# captitalized
words_cap = [word.capitalize() for word in words]
words_bound = " ".join(words_cap)
'''
*********** https://askubuntu.com/questions/166764/how-long-can-file-names-be ************
'''
words_bound = words_bound[:245]
title_with_space = words_bound
'''
********** END *******************8
'''
title_without_space = words_bound.replace(' ', '_')
new_name_pdf = title_without_space + ".pdf"
os.rename(filepath_obj.path, new_name_pdf)
filepath_obj_path = os.path.join(os.getcwd(), new_name_pdf)
print(new_name)
#captured_title = str(captured_title).strip().replace(".pdf","").replace("b'","").replace("\\n","").replace('\'',"")
'''
************************ END ***********************************************************
'''
temp_path = "to_delete/"+title_without_space
os.system(f"mkdir {temp_path}")
'''
************************* https://stackoverflow.com/a/65946983/5550255 ****************************
write the tile into a blank pdf page
'''
import fitz
# path = "PyMuPDF_test.pdf"
pdf3 = filepath_obj_path
pdf3_obj = fitz.open(pdf3)
old_first_page = pdf3_obj[0]
'''
*********** https://stackoverflow.com/a/64790804/5550255 ****************
'''
doc = fitz.open()
page = doc.new_page(width=old_first_page.rect.width, height=old_first_page.rect.height)
#where = fitz.Point(50, 50)
#page.insert_text(where, captured_title_normal, fontsize=20)
fontname_to_use = "Times-Roman"
fontsize_to_use = 20
text_lenght = fitz.get_text_length(title_with_space,
fontname=fontname_to_use,
fontsize=fontsize_to_use)
rect_x1 = 50
rect_y1 = 100
rect_x2 = rect_x1 + text_lenght + 2 # needs margin
rect_x2 = rect_x2 if rect_x2<=(old_first_page.rect.width -50) else old_first_page.rect.width -50
rect_y2 = rect_y1 + fontsize_to_use*math.ceil(text_lenght/(rect_x2-rect_x1))*2 + 2 # needs margin
rect = (rect_x1, rect_y1, rect_x2, rect_y2)
# Uncomment if you wish to display rect
page.draw_rect(rect,color=(.25,1,0.25))
rc = page.insert_textbox(rect, title_with_space,
fontsize=fontsize_to_use,
fontname=fontname_to_use,
align=0)
'''
***************** END ******************
'''
print(doc.metadata)
import copy
metadata = doc.metadata
metadata["producer"] = "paper_sorter"
doc.set_metadata(metadata) # clear all fields
print(doc.metadata)
doc.write() #?
os.system(f"mkdir {temp_path}")
pdf1 = f"{temp_path}/_only_title.pdf"
doc.save(pdf1)
doc.close()
pages = []
pages.append(pdf1)
'''
************************ END *******************
'''
from images import *
extract_image(new_name_pdf)
#some_magic(captured_title)
pdf2 = f"{temp_path}/extracted_images.pdf" # derived file from some_magic
if os.path.exists(pdf2):
open(pdf2)
pages.append(pdf2)
pages.append(pdf3)
'''
************************** https://stackabuse.com/working-with-pdfs-in-python-inserting-deleting-and-reordering-pages/ *******************
'''
import fitz
original_pdf = fitz.open(pdf1)
if os.path.exists(pdf2):
extra_page_1 = fitz.open(pdf2)
original_pdf.insert_pdf(extra_page_1)
extra_page_2 = fitz.open(pdf3)
original_pdf.insert_pdf(extra_page_2)
original_pdf.save(filepath_obj_path)
'''
************************************ END ********************
'''