This code takes the epub file and converts it into text
from bs4 import BeautifulSoup
import zipfile
def extract_text_from_xml(xml_content):
soup = BeautifulSoup(xml_content, 'html.parser')
paragraphs = soup.find_all('p')
extracted_text = [p.get_text() for p in paragraphs]
return extracted_text
def process_epub(epub_path, output_file):
with open(output_file, 'w', encoding='utf-8') as outfile:
with zipfile.ZipFile(epub_path, 'r') as epub_zip:
xml_files = [f for f in epub_zip.namelist() if f.endswith('.xml')]
for xml_file in xml_files:
with epub_zip.open(xml_file) as file:
extracted_text = extract_text_from_xml(file.read().decode())
for text in extracted_text:
outfile.write(text + '\n')
# Usage:
epub_path = 'epub/Observations.epub'
output_file = 'text/Bourgeois.txt'
process_epub(epub_path, output_file)