# -*- coding: utf-8 -*-
import xlrd
from xml.etree.ElementTree import Element, SubElement, ElementTree
def read_excel():
# 打开文件
workbook = xlrd.open_workbook(r'D:\odoo\od_message_config.xls')
sheet2 = workbook.sheet_by_name('Sheet 1')
# odoo标签
root = Element('odoo')
# data标签
data = SubElement(root, 'data')
for row in range(1, sheet2.nrows):
# 开始生成数据
row_value = sheet2.row_values(row)[1:]
print(row_value)
# 生成第一个子节点 record
record = SubElement(data, 'record', attrib={"model": "od.message.config", 'id': 'message_template_' + f"{row_value[1].lower()}"})
# record 节点的子节点 field
name_record = SubElement(record, 'field', attrib={"name": "name"})
name_record.text = row_value[0]
name_record = SubElement(record, 'field', attrib={"name": "code"})
name_record.text = row_value[1]
# name_record = SubElement(record, 'field', attrib={"name": "model_id"})
# name_record.text = row_value[2]
SubElement(record, 'field', attrib={'ref': f"{row_value[2]}", "name": "model_id"})
name_record = SubElement(record, 'field', attrib={"name": "model_name"})
name_record.text = row_value[3]
name_record = SubElement(record, 'field', attrib={"name": "mp_message"})
name_record.text = row_value[4]
record = SubElement(record, 'field', attrib={"name": "mp_copy_message"})
record.text = row_value[5]
tree = ElementTree(root)
tree.write(r'xustomer.xml', encoding='utf-8', xml_declaration=True)
if __name__ == '__main__':
read_excel()
python 生成odoo xml文件(2)
最新推荐文章于 2024-04-07 06:45:00 发布