Spaces:
Sleeping
Sleeping
Download backend/lib/parser.py from orakzai-io/Rednote: direct link, hf CLI and curl.
- Browser
- Download file 910 Bytes
-
https://huggingface.co/spaces/orakzai-io/Rednote/resolve/main/backend/lib/parser.py
- Command line
-
hf download hf://spaces/orakzai-io/Rednote/backend/lib/parser.py
-
curl -L -o parser.py https://huggingface.co/spaces/orakzai-io/Rednote/resolve/main/backend/lib/parser.py
910 Bytes
| # FUNCTIONS — parse_pdf(), parse_docx(), parse_txt() | |
| import fitz # type: ignore[import-untyped] # PyMuPDF | |
| import docx | |
| def parse_pdf(file_path: str) -> str: | |
| """Extracts text content from a PDF file using PyMuPDF.""" | |
| text_content: list[str] = [] | |
| with fitz.open(file_path) as doc: | |
| for page in doc: | |
| text: str = page.get_text() # type: ignore[assignment] | |
| if text: | |
| text_content.append(text) | |
| return "\n".join(text_content) | |
| def parse_docx(file_path: str) -> str: | |
| """Extracts text content from a DOCX file using python-docx.""" | |
| doc = docx.Document(file_path) | |
| return "\n".join([paragraph.text for paragraph in doc.paragraphs if paragraph.text]) | |
| def parse_txt(file_path: str) -> str: | |
| """Extracts text content from a plain text file.""" | |
| with open(file_path, "r", encoding="utf-8", errors="ignore") as f: | |
| return f.read() |