| 20 | |
| 21 | |
| 22 | class FileReader: |
| 23 | def __init__(self) -> None: |
| 24 | self._files_directory = ResultSetting().get_folder_output("files") |
| 25 | self._invalid_b64_chars = '!"#$%&\'()*,-.:;<>?@[\\]^_`{|}~ ' |
| 26 | self._magic_numbers = { |
| 27 | b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A": "png", |
| 28 | b"\xFF\xD8\xFF": "jpeg", |
| 29 | b"\x47\x49\x46\x38\x37\x61": "GIF", |
| 30 | b"\x50\x4B\x03\x04":"zip", |
| 31 | b"%PDF-":"pdf" |
| 32 | |
| 33 | } |
| 34 | |
| 35 | def _get_format(self, content:bytearray)->str: |
| 36 | for magic_file, file_type in self._magic_numbers.items(): |
| 37 | if content.startswith(magic_file): |
| 38 | return file_type |
| 39 | return None |
| 40 | |
| 41 | def _read_pdf(self, content:bytearray, max_pages=2)->str: |
| 42 | text = "" |
| 43 | pdf = io.BytesIO(content) |
| 44 | reader = PdfReader(pdf) |
| 45 | total_pages = len(reader.pages) |
| 46 | pages = min(total_pages, max_pages) |
| 47 | for page in reader.pages: |
| 48 | text+=page.extract_text()+'\n' |
| 49 | |
| 50 | return text |
| 51 | |
| 52 | def _read_word(self, zip_file, document_name)->str: |
| 53 | text="" |
| 54 | with zip_file.open(document_name) as xml: |
| 55 | |
| 56 | tree = ET.parse(xml) |
| 57 | root = tree.getroot() |
| 58 | for elem in root.iter("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t"): |
| 59 | if elem.text: |
| 60 | text += elem.text + "\n" |
| 61 | return text |
| 62 | |
| 63 | def _read_excel(self, content:bytearray)->str: |
| 64 | xlsx = BytesIO(content) |
| 65 | wb = load_workbook(xlsx) |
| 66 | ws = wb.active |
| 67 | text = '' |
| 68 | |
| 69 | for row in ws.iter_rows(values_only=True): |
| 70 | for cell in row: |
| 71 | if cell is not None: |
| 72 | text += str(cell) + ',' |
| 73 | |
| 74 | if len([v for v in row if v is not None])>0: |
| 75 | text+='\n' |
| 76 | |
| 77 | |
| 78 | return text |
| 79 | |