File size: 1,245 Bytes
ec55f11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
import fitz
from docx import Document
from crewai.tools import BaseTool
from pydantic import BaseModel, Field
from typing import Type


class CVParserInput(BaseModel):
    file_path: str = Field(description="Absolute path to the CV file (.pdf or .docx)")


class CVParserTool(BaseTool):
    name: str = "CV Parser"
    description: str = "Reads and extracts full text from a CV in PDF or DOCX format."
    args_schema: Type[BaseModel] = CVParserInput

    def _run(self, file_path: str) -> str:
        try:
            if file_path.endswith(".pdf"):
                return self._parse_pdf(file_path)
            elif file_path.endswith(".docx"):
                return self._parse_docx(file_path)
            else:
                return "Unsupported file format. Only PDF and DOCX supported."
        except Exception as e:
            return f"Error reading file: {str(e)}"

    def _parse_pdf(self, path: str) -> str:
        with fitz.open(path) as doc:
            text = ""
            for page in doc:
                text += page.get_text()
        return text.strip()

    def _parse_docx(self, path: str) -> str:
        doc = Document(path)
        text = "\n".join([para.text for para in doc.paragraphs])
        return text.strip()