File size: 5,584 Bytes
2f33d34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""

Resume file parser service.



This module handles extraction of text from resume files (PDF and DOCX).

It uses:

- pdfplumber: For efficient, accurate PDF text extraction

- python-docx: For reading Microsoft Word documents



The parser handles various edge cases like corrupt files,

unusual formatting, and validates content before returning.

"""

import logging
from typing import Optional
import pdfplumber
from docx import Document

logger = logging.getLogger(__name__)


class ResumeParser:
    """

    Handles resume file parsing (PDF and DOCX formats).

    

    This class provides methods to extract text from resume files

    while handling errors gracefully.

    """
    
    @staticmethod
    def parse_pdf(file_path: str) -> str:
        """

        Extract text from a PDF file.

        

        Uses pdfplumber which provides:

        - More accurate text extraction than PyPDF2

        - Better handling of tables and formatting

        - Direct access to page content

        

        Args:

            file_path: Path to the PDF file

            

        Returns:

            Extracted text from the PDF

            

        Raises:

            ValueError: If PDF is corrupted or cannot be read

            IOError: If file doesn't exist

        """
        try:
            text = ""
            with pdfplumber.open(file_path) as pdf:
                logger.info(f"Parsing PDF: {file_path} ({len(pdf.pages)} pages)")
                
                for page_num, page in enumerate(pdf.pages, 1):
                    try:
                        page_text = page.extract_text()
                        if page_text:
                            text += page_text + "\n"
                        logger.debug(f"Extracted text from page {page_num}")
                    except Exception as e:
                        logger.warning(f"Error extracting page {page_num}: {str(e)}")
                        continue
            
            if not text.strip():
                raise ValueError(
                    "No text content found in PDF. "
                    "This may be a scanned/image-based PDF. "
                    "Please use a PDF with selectable text or convert to DOCX format."
                )
            
            logger.info(f"Successfully extracted {len(text)} characters from PDF")
            return text
            
        except FileNotFoundError as e:
            logger.error(f"PDF file not found: {file_path}")
            raise IOError(f"Resume file not found: {file_path}")
        except Exception as e:
            logger.error(f"Error parsing PDF: {str(e)}")
            raise ValueError(f"Could not parse PDF file: {str(e)}")
    
    @staticmethod
    def parse_docx(file_path: str) -> str:
        """

        Extract text from a DOCX (Word) file.

        

        Uses python-docx to:

        - Extract paragraph text

        - Extract table content

        - Handle nested structures

        

        Args:

            file_path: Path to the DOCX file

            

        Returns:

            Extracted text from the DOCX

            

        Raises:

            ValueError: If DOCX is corrupted or cannot be read

            IOError: If file doesn't exist

        """
        try:
            doc = Document(file_path)
            logger.info(f"Parsing DOCX: {file_path}")
            
            text = ""
            
            # Extract from paragraphs using the safe, built-in method
            for para in doc.paragraphs:
                if para.text.strip():
                    text += para.text + "\n"
            
            # Extract from tables
            for table in doc.tables:
                for row in table.rows:
                    row_text = ""
                    for cell in row.cells:
                        if cell.text.strip():
                            row_text += cell.text + " "
                    if row_text.strip():
                        text += row_text + "\n"
            
            if not text.strip():
                raise ValueError("No text content found in DOCX")
            
            logger.info(f"Successfully extracted {len(text)} characters from DOCX")
            return text
            
        except FileNotFoundError as e:
            logger.error(f"DOCX file not found: {file_path}")
            raise IOError(f"Resume file not found: {file_path}")
        except Exception as e:
            logger.error(f"Error parsing DOCX: {str(e)}")
            raise ValueError(f"Could not parse DOCX file: {str(e)}")
    
    @staticmethod
    def parse_resume(file_path: str, file_type: str) -> str:
        """

        Parse resume file based on type.

        

        Determines which parser to use based on file extension,

        then returns extracted text.

        

        Args:

            file_path: Path to the resume file

            file_type: File type ('pdf' or 'docx')

            

        Returns:

            Extracted resume text

            

        Raises:

            ValueError: If file type is unsupported or file cannot be parsed

        """
        file_type = file_type.lower()
        
        if file_type == 'pdf':
            return ResumeParser.parse_pdf(file_path)
        elif file_type in ['docx', 'doc']:
            return ResumeParser.parse_docx(file_path)
        else:
            raise ValueError(f"Unsupported file type: {file_type}. Supported types: pdf, docx")