-
Notifications
You must be signed in to change notification settings - Fork 0
/
translation_extractor.py
executable file
·37 lines (30 loc) · 1.18 KB
/
translation_extractor.py
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
import os
import re
import openpyxl
class TextExtractor:
pattern = re.compile(r'![sdw]\d+')
def extract_text(self, file_path: str):
(file_directory, file_name) = os.path.split(file_path)
(file_name_only, ext) = os.path.splitext(file_name)
wb = openpyxl.Workbook()
wb.remove(wb.active)
with open(file_path, 'r', encoding='utf-8') as f:
ws = wb.create_sheet(file_name_only)
while True:
line = f.readline()
if not line:
break
line = line.strip()
if not line:
continue
last_char = line[len(line) - 1]
if last_char != '@' and last_char != '\\':
continue
line = line.rstrip("@\\")
line = line.replace('「', '\\\"').replace('」', '\\\"')
line = line.replace('!sd', '')
line = self.pattern.sub('', line)
for part in line.split('@'):
ws.append([part])
wb.save(os.path.join(file_directory, file_name_only + '.xlsx'))
wb.close()