提交 d9a3a6ba 编写于 作者: L linxinfa

Excel生成xml、lua和json

上级 ee708025
from genericpath import exists
import openpyxl
import os
def excel2xml(excel_path, xml_path):
book = openpyxl.load_workbook(excel_path)
# 默认只转第一个sheet
sheet_name = book.sheetnames[0]
sheet = book[sheet_name]
# 行数
max_row = sheet.max_row
# 列数
max_col = sheet.max_column
xml_txt = '<?xml version="1.0" encoding="utf-8">\n'
xml_txt += "<root>\n"
# 属性名
prop_names = {}
# 一行一行遍历
for row in range(max_row):
row += 1
if 2 == row:
for col in range(max_col):
col += 1
prop_names[col] = sheet.cell(row, col).value
elif 3 <= row:
xml_line = " <item "
for col in range(max_col):
col += 1
value = sheet.cell(row, col).value
value = "" if None == value else value
xml_line += '%s="%s" '%(prop_names[col], value)
xml_line += '/>\n'
xml_txt += xml_line
xml_txt += "</root>"
# 保存为xml
xml_dir = os.path.dirname(xml_path)
if not exists(xml_dir):
os.makedirs(xml_dir)
xml_f = open(xml_path, 'w', encoding='utf-8')
xml_f.write(xml_txt)
xml_f.close()
if '__main__' == __name__:
excel2xml(u"Excels/测试.xlsx", "./output/test.xml")
excel2xml(u"Excels/测试2.xlsx", "./output/test2.xml")
\ No newline at end of file
import exceltool
if '__main__' == __name__:
exceltool.excel2json(u"Excels/测试.xlsx", "./output/test.json")
exceltool.excel2json(u"Excels/测试2.xlsx", "./output/test2.json")
print('done!')
\ No newline at end of file
import exceltool
if '__main__' == __name__:
exceltool.excel2lua(u"Excels/测试.xlsx", "./output/test.lua")
exceltool.excel2lua(u"Excels/测试2.xlsx", "./output/test2.lua")
print('done!')
\ No newline at end of file
import exceltool
if '__main__' == __name__:
exceltool.excel2xml(u"Excels/测试.xlsx", "./output/test.xml")
exceltool.excel2xml(u"Excels/测试2.xlsx", "./output/test2.xml")
print('done!')
\ No newline at end of file
from genericpath import exists
import openpyxl
import os
import json2lua
import json
# 遍历excel的单元格
def walk_excel_cell(excel_path):
book = openpyxl.load_workbook(excel_path)
# 默认只转第一个sheet
sheet_name = book.sheetnames[0]
sheet = book[sheet_name]
# 行数
max_row = sheet.max_row
# 列数
max_col = sheet.max_column
# 属性名
field_names = {}
# 数据类型
data_types = {}
# 一行一行遍历
for row in range(max_row):
row += 1
if 2 == row:
for col in range(max_col):
col += 1
field_names[col] = sheet.cell(row, col).value
elif 3 == row:
for col in range(max_col):
col += 1
data_types[col] = sheet.cell(row, col).value
elif 4 <= row:
for col in range(max_col):
col += 1
value = sheet.cell(row, col).value
value = "" if None == value else value
yield col, max_col, field_names[col], data_types[col], value
def save_cfg(f_path, f_content):
f_dir = os.path.dirname(f_path)
if not exists(f_dir):
os.makedirs(f_dir)
f = open(f_path, 'w', encoding='utf-8')
f.write(f_content)
f.close()
def excel2xml(excel_path, xml_path):
xml_txt = '<?xml version="1.0" encoding="utf-8">\n'
xml_txt += "<root>\n"
xml_line = ""
for col, max_col, field_name, _, value in walk_excel_cell(excel_path):
if 1 == col:
xml_line = ' <item %s="%s"' % (field_name, value)
elif col == max_col:
xml_line += ' %s="%s"/>\n' % (field_name, value)
xml_txt += xml_line
else:
xml_line += ' %s="%s"' % (field_name, value)
xml_txt += "</root>"
# 保存为xml
save_cfg(xml_path, xml_txt)
print('excel2xml:', excel_path, '--->', xml_path)
def excel2lua(excel_path, lua_path):
li = []
item = None
for col, max_col, field_name, data_type, value in walk_excel_cell(excel_path):
if 1 == col:
item = {}
item[field_name] = (data_type, value)
elif col == max_col:
item[field_name] = (data_type, value)
li.append(item)
else:
item[field_name] = (data_type, value)
lua_txt = 'return\n' + json2lua.dic_to_lua_str(li)
# 保存为lua
save_cfg(lua_path, lua_txt)
print('excel2lua:', excel_path, '--->', lua_path)
def excel2json(excel_path, json_path):
li = []
item = None
for col, max_col, field_name, _, value in walk_excel_cell(excel_path):
if 1 == col:
item = {}
item[field_name] = value
elif col == max_col:
item[field_name] = value
li.append(item)
else:
item[field_name] = value
# 保存为lua
json_txt = json.dumps(li, ensure_ascii=False, sort_keys=True, indent=4)
save_cfg(json_path, json_txt)
print('excel2json:', excel_path, '--->', json_path)
\ No newline at end of file
python excel2xml.py
python excel2lua.py
python excel2json.py
pause
\ No newline at end of file
def space_str(layer):
lua_str = ""
for i in range(0, layer):
lua_str += ' '
return lua_str
def dic_to_lua_str(data, layer=0):
d_type = type(data)
if d_type is tuple:
if 'str' == data[0]:
return "'" + data[1] + "'"
elif 'num' == data[0]:
return str(data[1])
elif 'lua' == data[0]:
return '{}' if '' == data[1] else str(data[1])
elif d_type is list:
lua_str = "{"
lua_str += space_str(layer+1)
for i in range(0, len(data)):
lua_str += dic_to_lua_str(data[i], layer+1)
if i < len(data)-1:
lua_str += ','
lua_str += '\n'
lua_str += space_str(layer)
lua_str += '}'
return lua_str
elif d_type is dict:
lua_str = ''
lua_str += "\n"
lua_str += space_str(layer)
lua_str += "{\n"
data_len = len(data)
data_count = 0
for k, v in data.items():
data_count += 1
lua_str += space_str(layer+1)
if type(k) is int:
lua_str += '[' + str(k) + ']'
else:
lua_str += k
lua_str += ' = '
try:
lua_str += dic_to_lua_str(v, layer + 1)
if data_count < data_len:
lua_str += ',\n'
except Exception as e:
print('error in ', k, v)
raise
lua_str += '\n'
lua_str += space_str(layer)
lua_str += '}'
return lua_str
else:
print(d_type, 'is error')
return None
[
{
"attr": "{{1,700},{2,800},{3,900}}",
"desc": "金属性的战士才能挥舞的利剑",
"id": 1001,
"level": 85,
"name": "金之剑"
},
{
"attr": "{{1,700},{2,800},{3,901}}",
"desc": "木属性的战士才能挥舞的利剑",
"id": 1002,
"level": 85,
"name": "木之剑"
},
{
"attr": "{{1,700},{2,800},{3,902}}",
"desc": "水属性的战士才能挥舞的利剑",
"id": 1003,
"level": 85,
"name": "水之剑"
},
{
"attr": "{{1,700},{2,800},{3,903}}",
"desc": "火属性的战士才能挥舞的利剑",
"id": 1004,
"level": 80,
"name": "火之剑"
},
{
"attr": "{{1,700},{2,800},{3,903}}",
"desc": "土属性的战士才能挥舞的利剑",
"id": 1005,
"level": 75,
"name": "土之剑"
},
{
"attr": "",
"desc": "这件装饰没有特殊的能力,但非常好看",
"id": 1006,
"level": 18,
"name": "空饰"
}
]
\ No newline at end of file
return
{
{
id = 1001,
level = 85,
name = '金之剑',
attr = {{1,700},{2,800},{3,900}},
desc = '金属性的战士才能挥舞的利剑'
},
{
id = 1002,
level = 85,
name = '木之剑',
attr = {{1,700},{2,800},{3,901}},
desc = '木属性的战士才能挥舞的利剑'
},
{
id = 1003,
level = 85,
name = '水之剑',
attr = {{1,700},{2,800},{3,902}},
desc = '水属性的战士才能挥舞的利剑'
},
{
id = 1004,
level = 80,
name = '火之剑',
attr = {{1,700},{2,800},{3,903}},
desc = '火属性的战士才能挥舞的利剑'
},
{
id = 1005,
level = 75,
name = '土之剑',
attr = {{1,700},{2,800},{3,903}},
desc = '土属性的战士才能挥舞的利剑'
},
{
id = 1006,
level = 18,
name = '空饰',
attr = {},
desc = '这件装饰没有特殊的能力,但非常好看'
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8">
<root>
<item id="1001" level="85" name="金之剑" attr="{{1,700},{2,800},{3,900}}" desc="金属性的战士才能挥舞的利剑" />
<item id="1002" level="85" name="木之剑" attr="{{1,700},{2,800},{3,901}}" desc="木属性的战士才能挥舞的利剑" />
<item id="1003" level="85" name="水之剑" attr="{{1,700},{2,800},{3,902}}" desc="水属性的战士才能挥舞的利剑" />
<item id="1004" level="80" name="火之剑" attr="{{1,700},{2,800},{3,903}}" desc="火属性的战士才能挥舞的利剑" />
<item id="1005" level="75" name="土之剑" attr="{{1,700},{2,800},{3,903}}" desc="土属性的战士才能挥舞的利剑" />
<item id="1006" level="18" name="空饰" attr="" desc="这件装饰没有特殊的能力,但非常好看" />
<item id="1001" level="85" name="金之剑" attr="{{1,700},{2,800},{3,900}}" desc="金属性的战士才能挥舞的利剑"/>
<item id="1002" level="85" name="木之剑" attr="{{1,700},{2,800},{3,901}}" desc="木属性的战士才能挥舞的利剑"/>
<item id="1003" level="85" name="水之剑" attr="{{1,700},{2,800},{3,902}}" desc="水属性的战士才能挥舞的利剑"/>
<item id="1004" level="80" name="火之剑" attr="{{1,700},{2,800},{3,903}}" desc="火属性的战士才能挥舞的利剑"/>
<item id="1005" level="75" name="土之剑" attr="{{1,700},{2,800},{3,903}}" desc="土属性的战士才能挥舞的利剑"/>
<item id="1006" level="18" name="空饰" attr="" desc="这件装饰没有特殊的能力,但非常好看"/>
</root>
\ No newline at end of file
[
{
"attr": "[[1,700],[2,800],[3,900]]",
"count": 2,
"id": 1,
"type": 1
},
{
"attr": "[[1,700],[2,800],[3,901]]",
"count": 4,
"id": 2,
"type": 3
},
{
"attr": "[[1,700],[2,800],[3,902]]",
"count": 6,
"id": 3,
"type": 5
},
{
"attr": "[[1,700],[2,800],[3,903]]",
"count": 8,
"id": 4,
"type": 7
},
{
"attr": "[[1,700],[2,800],[3,904]]",
"count": 10,
"id": 5,
"type": 9
},
{
"attr": "[[1,700],[2,800],[3,904]]",
"count": 10,
"id": 5,
"type": 11
}
]
\ No newline at end of file
return
{
{
id = 1,
type = 1,
count = 2,
attr = [[1,700],[2,800],[3,900]]
},
{
id = 2,
type = 3,
count = 4,
attr = [[1,700],[2,800],[3,901]]
},
{
id = 3,
type = 5,
count = 6,
attr = [[1,700],[2,800],[3,902]]
},
{
id = 4,
type = 7,
count = 8,
attr = [[1,700],[2,800],[3,903]]
},
{
id = 5,
type = 9,
count = 10,
attr = [[1,700],[2,800],[3,904]]
},
{
id = 5,
type = 11,
count = 10,
attr = [[1,700],[2,800],[3,904]]
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8">
<root>
<item id="1" type="1" count="2" attr="[[1,700],[2,800],[3,900]]" />
<item id="2" type="3" count="4" attr="[[1,700],[2,800],[3,901]]" />
<item id="3" type="5" count="6" attr="[[1,700],[2,800],[3,902]]" />
<item id="4" type="7" count="8" attr="[[1,700],[2,800],[3,903]]" />
<item id="5" type="9" count="10" attr="[[1,700],[2,800],[3,904]]" />
<item id="5" type="11" count="10" attr="[[1,700],[2,800],[3,904]]" />
<item id="1" type="1" count="2" attr="[[1,700],[2,800],[3,900]]"/>
<item id="2" type="3" count="4" attr="[[1,700],[2,800],[3,901]]"/>
<item id="3" type="5" count="6" attr="[[1,700],[2,800],[3,902]]"/>
<item id="4" type="7" count="8" attr="[[1,700],[2,800],[3,903]]"/>
<item id="5" type="9" count="10" attr="[[1,700],[2,800],[3,904]]"/>
<item id="5" type="11" count="10" attr="[[1,700],[2,800],[3,904]]"/>
</root>
\ No newline at end of file
这是用python写的一个将Excel转xml、lua、json的工具
Excel文件放在Excels目录中,生成的配置会放在output文件夹中
Excel表头格式
第一行:字段备注
第二行:字段名
第三行:数据类型,num、str、lua
自行在excel2xml.py、excel2lua.py、excel2json.py中指定你要转换的excel
最终双击gen.bat脚本执行即可
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册