Spaces:
Build error

AmazonVM / process_docs.py
mukulmitra82's picture
Create process_docs.py
b4e2000 verified
Raw History Blame Contribute Delete
927 Bytes
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