-
Notifications
You must be signed in to change notification settings - Fork 0
/
Excel2Xliff.py
308 lines (265 loc) · 10.5 KB
/
Excel2Xliff.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/python
# -*- coding: utf-8 -*-
#### DEBUGGING ####
# import pdb; pdb.set_trace()
#### SCRIPT VERSION ####
## 0.2
######## LOAD MODULES ########
import subprocess
import sys
try:
from tkinter import *
from tkinter.filedialog import *
from tkinter import font
from ttkthemes import ThemedStyle
import pandas as pd
import xlrd
import os
import getpass
import time
print("Your Python version is:", sys.version)
print("\nAll modules are loaded!\n")
except ImportError:
subprocess.call([sys.executable, "-m", "pip", "install", 'pandas'])
subprocess.call([sys.executable, "-m", "pip", "install", 'xlrd'])
subprocess.call([sys.executable, "-m", "pip", "install", 'ttkthemes'])
print("\nAll modules are being installed...")
finally:
from tkinter import *
from tkinter.filedialog import *
from tkinter import font
from ttkthemes import ThemedStyle
import pandas as pd
import xlrd
import os
import getpass
import time
print("\nContinue...")
time.sleep(2)
######## WORKING DIRECTORY ########
username = getpass.getuser()
os.chdir('/Users/' + username + '/Desktop')
time.sleep(2)
################################################################################################################
######## TKINTER ########
window = Tk()
#### WINDOW ####
window.title("Excel2XLIFF App")
window.geometry('650x200')
window.resizable(height=False, width=False)
#### THEME ####
style = ThemedStyle(window)
style.set_theme("aqua")
#### FONT ####
appHighlightFont = font.Font(family='Arial', size=12)
window.option_add("*Font", appHighlightFont)
######## CONTENT ########
lbl = Label(window, text="")
lbl.grid(column=0, row=0, sticky=E)
#### LABEL ####
lbl1 = Label(window, text="Enter Excel file path here", font="Arial 14 bold")
lbl1.grid(column=0, row=0, sticky=W, padx=15, pady=25)
lbl2 = Label(window, text="Enter original XLIFF file path here", font="Arial 14 bold")
lbl2.grid(column=0, row=1, sticky=W, padx=15)
#### INPUT ####
input1 = Entry(window,width=50)
input1.grid(column=1, row=0)
input2 = Entry(window,width=50)
input2.grid(column=1, row=1)
#### RADIO BUTTONS ####
check_language = StringVar()
check_language.set("pt-BR")
rbutton_pt = Radiobutton(window,text="Portuguese",variable=check_language,value="pt-BR", font=('Arial', 14)).grid(column=0,row=4,sticky=E,pady=30)
rbutton_es = Radiobutton(window,text="Spanish",variable=check_language,value="es", font=('Arial', 14)).grid(column=1,row=4,sticky=W,pady=30)
################################################################################################################
#### CONVERTING FUNCTION ####
def translate_XLIFF():
excel_file = input1.get()
xliff_file = input2.get()
language_input = check_language.get()
############### LOAD EXCEL FILE ###############
data = pd.read_excel(excel_file)
# Make sure all Data types are string
data["Title"]= data["Title"].astype(str)
data["Body"]= data["Body"].astype(str)
data["Body_2"]= data["Body_2"].astype(str)
data["Body_3"]= data["Body_3"].astype(str)
data["Link"]= data["Link"].astype(str)
# Fill NAs with 0
data.fillna(0, inplace = True)
# Load en-US XLIFF file
contents = ""
file = xliff_file
with open(file) as f:
for line in f.readlines():
contents += line
# Get original file name
old_file_name = os.path.basename(file)
############### FILE NAME ###############
# Create new file name
lenght = len(old_file_name)
old_file_name_index = contents.find(old_file_name)
old_file_name_index = old_file_name_index - 10
new_file_name_FINAL = old_file_name[:old_file_name_index] + language_input
############### LANGUAGE ###############
language = language_input
index_language = contents.find('target-language="')
y_language = len('target-language="')
x_language = index_language + y_language
output_line = contents[:index_language] + contents[index_language:x_language] + language + contents[x_language:]
# Save file
with open(new_file_name_FINAL + '.xliff', 'w') as file:
file.write(output_line)
# Load file
contents = ""
file = new_file_name_FINAL + '.xliff'
with open(file) as f:
for line in f.readlines():
contents += line
############### TITLE CONDITION ###############
if language_input == 'pt-BR':
data_title = data['Title'][1]
title_EN = str(data['Title'][0])
if data_title != 0 and title_EN in contents:
# TITLE CONTENT HERE
title = str(data_title)
index_title = contents.find(title_EN)
y_title = len(title_EN)
x_title = index_title + y_title + len('**</source><target>')
output_line = contents[:index_title] + contents[index_title:x_title] + '**' + title + '**' + contents[x_title:]
pass
elif language_input == 'es':
data_title = data['Title'][2]
title_EN = str(data['Title'][0])
if data_title != 0 and title_EN in contents:
# TITLE CONTENT HERE
title = str(data_title)
index_title = contents.find(title_EN)
y_title = len(title_EN)
x_title = index_title + y_title + len('**</source><target>')
output_line = contents[:index_title] + contents[index_title:x_title] + '**' + title + '**' + contents[x_title:]
pass
else:
print('Something went wrong!')
############### SAVE FILE WITH TITLE ###############
# Save file
with open(new_file_name_FINAL + '.xliff', 'w') as file:
file.write(output_line)
# Load file
contents = ""
file = new_file_name_FINAL + '.xliff'
with open(file) as f:
for line in f.readlines():
contents += line
############### BODY CONDITION ###############
if language_input == 'pt-BR':
data_body = data['Body'][1]
body_EN = str(data['Body'][0])
if data_body != 0 and body_EN in contents:
# BODY CONTENT HERE
body = str(data_body)
index_body = contents.find(body_EN)
y_body = len(body_EN)
x_body = index_body + y_body + len('</source><target>')
output_line = contents[:index_body] + contents[index_body:x_body] + data_body + contents[x_body:]
pass
elif language_input == 'es':
data_body = data['Body'][2]
body_EN = str(data['Body'][0])
if data_body != 0 and body_EN in contents:
# BODY CONTENT HERE
body = str(data_body)
index_body = contents.find(body_EN)
y_body = len(body_EN)
x_body = index_body + y_body + len('</source><target>')
output_line = contents[:index_body] + contents[index_body:x_body] + data_body + contents[x_body:]
pass
else:
print('Please insert either "pt-BR" or "es" only.')
############### SAVE FILE WITH BODY ###############
# Save file
with open(new_file_name_FINAL + '.xliff', 'w') as file:
file.write(output_line)
# Load file
contents = ""
file = new_file_name_FINAL + '.xliff'
with open(file) as f:
for line in f.readlines():
contents += line
############### BODY_2 CONDITION ###############
if language_input == 'pt-BR':
data_body_2 = data['Body_2'][1]
body_EN_2 = str(data['Body_2'][0])
if data_body_2 != 0 and body_EN_2 in contents:
# BODY_2 CONTENT HERE
body_2 = str(data_body_2)
index_body_2 = contents.find(body_EN_2)
y_body_2 = len(body_EN_2)
x_body_2 = index_body_2 + y_body_2 + len('</source><target>')
output_line = contents[:index_body_2] + contents[index_body_2:x_body_2] + body_2 + contents[x_body_2:]
pass
elif language_input == 'es':
data_body_2 = data['Body_2'][2]
body_EN_2 = str(data['Body_2'][0])
if data_body_2 != 0 and body_EN_2 in contents:
# BODY_2 CONTENT HERE
body_2 = str(data_body_2)
index_body_2 = contents.find(body_EN_2)
y_body_2 = len(body_EN_2)
x_body_2 = index_body_2 + y_body_2 + len('</source><target>')
output_line = contents[:index_body_2] + contents[index_body_2:x_body_2] + body_2 + contents[x_body_2:]
pass
else:
print('Please insert either "pt-BR" or "es" only.')
############### SAVE FILE WITH BODY_2 ###############
# Save file
with open(new_file_name_FINAL + '.xliff', 'w') as file:
file.write(output_line)
# Load file
contents = ""
file = new_file_name_FINAL + '.xliff'
with open(file) as f:
for line in f.readlines():
contents += line
############### LINK CONDITION ###############
if language_input == 'pt-BR':
data_link = data['Link'][1]
link_EN = str(data['Link'][0])
if data_link != 0 and link_EN in contents:
# LINK CONTENT HERE
link = str(data_link)
index_link = contents.find(link_EN)
y_link = len(link_EN)
x_link = index_link + y_link + len('</source><target>')
output_line = contents[:index_link] + contents[index_link:x_link] + link + contents[x_link:]
pass
elif language_input == 'es':
data_link = data['Link'][2]
link_EN = str(data['Link'][0])
if data_link != 0 and link_EN in contents:
# LINK CONTENT HERE
link = str(data_link)
index_link = contents.find(link_EN)
y_link = len(link_EN)
x_link = index_link + y_link + len('</source><target>')
output_line = contents[:index_link] + contents[index_link:x_link] + link + contents[x_link:]
pass
else:
print('Please insert either "pt-BR" or "es" only.')
############### REPLACE HTML CHARACTERS ###############
# Replace special characters with HTML Entity Name
output_line = output_line.replace(" & ", " & ")
output_line = output_line.replace("›", "›")
############### SAVE FILE WITH LINK ###############
# Save file
with open(new_file_name_FINAL + '.xliff', 'w') as file:
file.write(output_line)
print("\nFile saved on Desktop!")
del output_line
del contents
################################################################################################################
#### BUTTON ####
btn = Button(window, text="Convert Files", fg="black", command=translate_XLIFF, width=25, font=('Arial', 14, 'bold'), bd=8, relief=RAISED)
btn.grid(column=1, row=4, sticky=E, pady=30)
#### END ####
window.mainloop()