-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlsx_to_po.py
84 lines (77 loc) · 2.76 KB
/
xlsx_to_po.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
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
import os
import xlwings as xw
def write_line(content: str, txt, newline=False, quote=True, comment=False):
if content.endswith('\n'):
if comment:
content = content[:len(content) - 1]
else:
content = content[:len(content) - 2]
if quote:
txt.write(content + '\"\n')
else:
txt.write(content + '\n')
if newline:
txt.write('\n')
elif content.endswith('\\n'):
if quote:
txt.write(content + '\"\n')
else:
txt.write(content + '\n')
if newline:
txt.write('\n')
else:
if quote:
txt.write(content + '\"\n')
else:
txt.write(content + '\n')
if newline:
txt.write('\n')
def isfloat(x):
try:
float(x)
except ValueError:
return False
else:
return True
if __name__ == '__main__':
print('xlsx转po工具 by mirage')
location = input('输入xlsx文件路径:')
app = xw.App(visible=False, add_book=False)
wb = app.books.open(location)
sht = xw.sheets[0]
length = len(sht.used_range.value) - 1
print('共有' + str(length) + '行')
new_filename = os.path.basename(location).rpartition('.')[0] + '.po'
f = open(new_filename, 'a', encoding='utf-8')
f.write('msgid \"\"\n')
f.write('msgstr \"\"\n')
f.write('\"MIME-Version: 1.0\\n\"\n')
f.write('\"Content-Type: text/plain; charset=UTF-8\\n\"\n')
f.write('\"Content-Transfer-Encoding: 8bit\\n\"\n\n')
for i in range(2, length + 2):
print('正在处理第' + str(i - 1) + '行')
if sht.range('A' + str(i)).value is not None:
file_info = str(sht.range('A' + str(i)).value)
write_line('#: ' + file_info, f, quote=False, comment=True)
if sht.range('B' + str(i)).value is not None:
msgctxt = sht.range('B' + str(i)).value
if isfloat(msgctxt):
msgctxt_int = int(float(msgctxt))
msgctxt = msgctxt_int
write_line('msgctxt \"' + str(msgctxt), f)
if sht.range('C' + str(i)).value is not None:
source = str(sht.range('C' + str(i)).value)
if isfloat(source):
source_int = int(float(source))
source = source_int
write_line('msgid \"' + str(source), f)
if sht.range('D' + str(i)).value is not None:
target = str(sht.range('D' + str(i)).value)
if isfloat(target):
target_int = int(float(target))
target = target_int
write_line('msgstr \"' + str(target), f, newline=True)
f.close()
wb.close()
print(f'{location}处理完成')
print(f'文件已保存至:{os.path.abspath(new_filename)}')