Spaces:
Build error
Build error
File size: 927 Bytes
b4e2000 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import os
import docx
import glob
def extract_text_from_txt(file_path):
"""Extract text from a .txt file."""
with open(file_path, "r", encoding="utf-8") as f:
return f.read()
def extract_text_from_docx(file_path):
"""Extract text from a .docx file."""
doc = docx.Document(file_path)
return "\n".join([para.text for para in doc.paragraphs])
def load_documents(data_folder="data"):
"""Load and process text from .txt and .docx files."""
all_text = ""
for file_path in glob.glob(os.path.join(data_folder, "*.txt")):
all_text += extract_text_from_txt(file_path) + "\n\n"
for file_path in glob.glob(os.path.join(data_folder, "*.docx")):
all_text += extract_text_from_docx(file_path) + "\n\n"
return all_text
# Example usage:
if __name__ == "__main__":
text_data = load_documents()
print("Extracted Text:", text_data[:500]) # Show the first 500 characters
|