INNER CODE UNIT · Python
process_files
cognitivetech/ollama-ebook-summary · book2text.py:125
def process_files(directory, file_type):
data = []
print(directory)
files = sorted(os.listdir(directory), key=natural_sort_key)
for filename in files:
filepath = os.path.join(directory, filename)
if file_type == 'html' and filename.endswith('.html'):
text = html_to_text(filepath)
title = get_title_from_html(filepath)
elif file_type == 'epub':
try:
text = epub_to_text(filepath)
book = epub.read_epub(filepath)
title = book.get_metadata('DC', 'title')[0][0]
except Exception as e:
print(f"Error processing {filename}: {str(e)}")
elif file_type == 'pdf' and filename.endswith('.pdf'):
text = pdf_to_text(filepath)