merge_tex.py 873 B

12345678910111213141516171819202122232425262728293031
  1. import os
  2. import re
  3. def open_and_read(in_file):
  4. with open(in_file, "r") as in_file:
  5. lines = in_file.readlines()
  6. return lines
  7. def include_sub_files(in_file, my_text="", exclude_comments=True):
  8. lines = open_and_read(in_file)
  9. for line in lines:
  10. m = re.match("^\\\\input{(?P<sub_file>.+)}",line)
  11. if m:
  12. sub_file = m.groupdict()['sub_file']
  13. my_text = include_sub_files(sub_file,
  14. my_text=my_text,
  15. exclude_comments=exclude_comments,
  16. )
  17. else:
  18. if exclude_comments:
  19. if line.startswith("%"):
  20. continue
  21. my_text += line
  22. return my_text
  23. out_text = include_sub_files('article-frontiers.tex')
  24. with open("article-frontiers-onefile.tex", "w") as out_file:
  25. out_file.write(out_text)
  26. out_file.close()