-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_images_to_compress.py
32 lines (29 loc) · 1.13 KB
/
update_images_to_compress.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
# Import the os module, for the os.walk function
import os
# Set the directory you want to start from
rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir):
#print('Found directory: %s' % dirName)
for fname in fileList:
if ".tex" in fname:
#print('\t%s' % fname)
# Read input file
fin = open(os.path.join(dirName, fname), "rt")
# Read file contents to string
data = fin.readlines()
# Replace all occurrences of the required string
aux_string = ""
for line in data:
if ".pdf" in line:
if not "pdfcompress" in line:
aux_string += line.replace("\includegraphics[","\includegraphics[pdfcompress,")
else:
aux_string += line
# Close the input file
fin.close()
# Open the input file in write mode
fin = open(os.path.join(dirName, fname), "wt")
# Overrite the input file with the resulting data
fin.write(aux_string)
# Close the file
fin.close()