From 28548497290fc1478d7d88af4fc756a5a337c33d Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 13 May 2025 17:05:30 +0800 Subject: [PATCH] bugfix --- file2text/loader.py | 20 ++++++++++++++++---- requirements.txt | 1 + 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/file2text/loader.py b/file2text/loader.py index 38cf5a2..6b57ce6 100644 --- a/file2text/loader.py +++ b/file2text/loader.py @@ -4,6 +4,7 @@ from datetime import datetime from langchain_community.document_loaders.csv_loader import CSVLoader from langchain_community.document_loaders.text import TextLoader from docx import Document +import doc # import chm from ebooklib import epub from bs4 import BeautifulSoup @@ -60,7 +61,17 @@ class MyPdfLoader: text += page.extract_text() or '' return ' '.join(text.split('\t')) -class MyWordLoader: +class MyDocLoader: + def __init__(self, file_path): + self.filepath = file_path + + def load(self): + """Extract plain text from a .doc file using python-doc.""" + with open(self.filepath, 'rb') as f: + document = doc.Document(f) + return document.text() + +class MyDocxLoader: def __init__(self, file_path): self.filepath = file_path self.document = None @@ -134,10 +145,11 @@ def fileloader(file_path): # Load the PDF file and split the data into chunks data = None if file_path.lower().endswith('.pdf'): - # loader = PyPDFLoader(file_path=file_path) loader = MyPdfLoader(file_path=file_path) - elif file_path.lower().endswith('.docx') or file_path.lower().endswith('.doc'): - loader = MyWordLoader(file_path=file_path) + elif file_path.lower().endswith('.docx'): + loader = MyDocxLoader(file_path=file_path) + elif file_path.lower().endswith('.doc'): + loader = MyDocLoader(file_path=file_path) elif file_path.lower().endswith('.pptx') or file_path.lower().endswith('.pptx'): loader = MyPptLoader(file_path=file_path) elif file_path.lower().endswith('.xlsx') or file_path.lower().endswith('.xls'): diff --git a/requirements.txt b/requirements.txt index ba5cbc3..4eb265c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ mobi html2text chm python-docx +python-doc python-pptx openpyxl PyPDF2