import glob import re from os.path import join import os import shutil try: from titlecase import titlecase except ImportError: print "Please install titlecase" regex = re.compile(r'\\(chapter|section|subsection)', re.IGNORECASE) EXEMPTS = () def exempter(line): for exempt in EXEMPTS: if line.startswith(exempt): return True return line in EXEMPTS def clean_line(line): line = line.replace('\\chapter{', '') line = line.replace('\\section{', '') line = line.replace('\\subsection{', '') line = line.replace('}', '') line = line.replace('[', '') line = line.replace(']', ' ') line = line.strip() return line def main(): for chaptername in glob.glob("chapters/[0-9][0-9]*.tex"): chap_number = chaptername[9:11] with open(chaptername) as f: lines = f.readlines() for line in lines: match = re.match(regex, line) if match is None: continue line = clean_line(line) if exempter(line): continue new_line = titlecase(line) if new_line != line: print "OLD: ", line print "NEW: ", new_line print "=====================" if __name__ == "__main__": main()