Hello! You do not need the table of contents. You can get the chapter name and use it.
Here is example script which loads the file lines into a list. Read each line and looks for 'CHAPTER' in the line. Then if the next line is empty, it gets the next line and this is the file name. Writes to the file until two new lines are reached.
Here is example script which loads the file lines into a list. Read each line and looks for 'CHAPTER' in the line. Then if the next line is empty, it gets the next line and this is the file name. Writes to the file until two new lines are reached.
while book: line = book.pop(0) if "CHAPTER" in line and book.pop(0) == '\n': chapter = line.strip() title = book.pop(0).strip("\n\r\ .") with open("{} - {}.txt".format(chapter,title), 'w') as f: print('Writing: "{} - {}.txt"'.format(chapter,title)) new_lines = 0 try: while new_lines < 3: row = book.pop(0) if row == '\n': f.write(row) new_lines += 1 else: f.write(row) new_lines = 0 except IndexError: passHm! The indentation could be messy.