File size: 1,268 Bytes
f807e7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import unicodedata
import re
import logging

import docx2txt

from gpt_processor import Translator

class DOCXProcessor:
    def __init__(self, file_path: str) -> None:
        self.file_path = file_path
        self.file_info = {
            'file_name': self.file_path.split('/')[-1],
            'file_format': 'DOCX',
            'file_full_content': '',
        }
        self.__build_info()

    def __build_info(self) -> None:
        try:
            text = docx2txt.process(self.file_path)
            text = unicodedata.normalize("NFKD", text)
            text = text.replace('\n', ' ').replace('\r', '')
            text = re.sub(' +', ' ', text)
            self.file_info['is_chinese'] = self.__is_chinese(text)

            tranlator = Translator()
            self.file_info['file_full_content'] = tranlator.translate_to_chinese(text) if not self.file_info['is_chinese'] else text

                
        except FileNotFoundError:
            print(f"File not found: {self.file_path}")
        except Exception as e:
            print(f"An error occurred: {str(e)}")
        
    def __is_chinese(self, text: str) -> bool:
        for char in text:
            if char >= '\u4e00' and char <= '\u9fff':
                return True
        return False