-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstring_import_soft.py
68 lines (54 loc) · 2.21 KB
/
string_import_soft.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
# Strings.xml to excel export script by Mohammed Shahim
# Steps to use:
# 1. Have python installed
# 2. install dependencies: `pip3 install -r .\py_requirements.txt`
# 3. run the script: `python string_export.py`
# 4. Enjoy
# import csv
import os
import pandas as pd
from lxml import etree
# Load the Excel file
df = pd.read_excel("strings.xlsx")
# Iterate over each column in the row
for column in df.columns:
if column != "Name" and column != "values":
# Create the directory for the XML file if it doesn't exist
directory = f"./app/src/main/res/{column}"
os.makedirs(directory, exist_ok=True)
filepath = os.path.join(directory, "strings.xml")
srcTree = etree.parse(filepath)
root = srcTree.getroot()
# for string in :
# Find the last element
last_element = None
for element in root.iterfind("./string"):
last_element = element
if last_element is not None:
last_element.tail = "\n "
# Iterate over each row in the DataFrame
for _, row in df.iterrows():
# Get the string name
string_name = row["Name"]
# Get the string value
string_value = row[column]
# Skip if the string value is NaN
if pd.isna(string_value):
continue
# Create an XML element for the string
string_element = root.find("./string[@name='%s']" % string_name)
if string_element is not None:
string_element.text = string_value
else:
# Create an XML element for the string
string_element = etree.Element("string", {"name": string_name})
string_element.text = string_value
string_element.tail = "\n "
root.append(string_element)
last_element = None
for element in root.iterfind("./string"):
last_element = element
if last_element is not None:
last_element.tail = "\n"
srcTree.write(filepath, pretty_print=True, xml_declaration=True, encoding='utf-8')
print('Conversion completed. Skipped non-translatable strings. Check strings.csv for the output.')