python 自动对比取excel 覆盖string id

import xml.dom.minidom
from xlrd import open_workbook
from xlwt import Workbook
from xml.dom import minidom
import codecs

# 打开xml  
xmldoc = xml.dom.minidom.parse('stringen.xml')
code = xmldoc.getElementsByTagName('string')

# 打开excel
workbook = open_workbook('stringsen1.xls')

# 新建xml
doc = minidom.Document()
resources = doc.createElement('resources')
doc.appendChild(resources)
row = 1
for node in code:
    for item in node.childNodes:
        # xml  的id
        #print("开始查找相同的id=="+node.getAttribute('name')+"   value===="+ item.nodeValue)
        text_element = doc.createElement('string')
        text_element.setAttribute('name', node.getAttribute('name'))
        for sheet in workbook.sheets():
            for col in range(1,2):
                # 添加根元素
               
                temp = "null"
                for row_index in range(1,sheet.nrows):
                    result_placeholder = sheet.cell(row_index, 0).value
                    result_content = sheet.cell(row_index, col).value
                    # 新建一个文本元素
                    if node.getAttribute('name')==result_placeholder :
                        print("key相同"+node.getAttribute('name')+ "****"+result_placeholder)
                        #text_element.appendChild(doc.createTextNode(result_content))
                        #print("key相同"+item.nodeValue)
                        #resources.appendChild(text_element)
                        temp = result_content
                        break
                    #else :
                        #text_element.appendChild(doc.createTextNode(item.nodeValue))
                        #print(item.nodeValue)
                        #resources.appendChild(text_element)
                    #print("key==="+result_placeholder+"  ***valus===" + result_content)
                if temp == "null" :
                    #print("key相同"+item.nodeValue)
                    text_element.appendChild(doc.createTextNode(item.nodeValue))   
                else:
                    #print("key不相同"+item.nodeValue)
                    text_element.appendChild(doc.createTextNode(temp))   
                #result_content = sheet.cell(0, col).value
                resources.appendChild(text_element)
f = codecs.open("test"+'_new_strings.xml', 'w', encoding='utf-8')
f.write(doc.toprettyxml(indent='    '))
doc.removeChild(resources)

xml 跟xls  相同id把对应的翻译从 xls 复制到xml 上。

stringen.xml :android string 资源文件

stringsen1.xls   xls 更新的一些翻译(客户更新的)

test_new_strings.xml  生成新的string xml 翻译

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值