Skip to content

Instantly share code, notes, and snippets.

@samuelsaari
Last active July 1, 2022 11:02
Show Gist options
  • Save samuelsaari/99f3ffe35a63482698571d713e9ee594 to your computer and use it in GitHub Desktop.
Save samuelsaari/99f3ffe35a63482698571d713e9ee594 to your computer and use it in GitHub Desktop.
# modified from https://tex.stackexchange.com/a/162763/244928 by StackExhange user imriss
"""Copy figures and .tex-files used by document."""
import os
import shutil
import glob
import re
from pathlib import Path
os.chdir(os.path.dirname(__file__)) # working directory to file directory
new_folder='used_floats'
# deleting existing directory
try:
shutil.rmtree(new_folder)
except:
print ("Deletion of the directory %s failed" % new_folder)
else:
print ("Successfully deleted the directory %s" % new_folder)
# creating new directory
try:
os.mkdir(new_folder)
except OSError:
print ("Creation of the directory %s failed" % new_folder)
else:
print ("Successfully created the directory %s " % new_folder)
# extracting the right file to extract file names from##
for name in glob.glob('*Manuscript*.tex'): # contains "manuscript", ends with .tex
if re.search(r'^((?!Copy).)*$', name): # does not contain "Copy"
print(f'--tex file to use:{name}--')
DEP_FILE =Path(name).stem + ".dep"
print(f'--dep file to use:{DEP_FILE}--')
else:
pass
TARGET_DIR = new_folder + '/'
EXTENSIONS = ['pdf', 'pdf_tex', 'png','tex']
def copy_image_files():
with open(DEP_FILE, 'r') as f:
for line in f:
if '*{file}' not in line:
continue
value = line.split('{')[2].split('}')
#print(f'value: {value}')
source = value[0]
_, e = os.path.splitext(source)
e = e.lower()[1:]
# print(f'e: {e}')
if re.search(r'\.code\.tex',source) or source=="xkeyval.tex":
print(f'exclude auxiliary file: {source}')
continue
elif e not in EXTENSIONS:
continue
print(f'Moving: {source}')
shutil.copy(source, TARGET_DIR)
print('-------moving the following files--------')
if __name__ == '__main__':
copy_image_files()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment