环境:
    
    1、PyCharm 2022.2.2 (Community Edition)
    
    2、xlrd-2.0.1.tar
    
    3、xlwt-1.3.0.tar
    
    4、pip
   
    下载地址:
    
    1、https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=windows&code=PCC
    
    2、https://pypi.org/project/xlwt/#files
    
    3、https://pypi.org/project/xlrd/#files
    
    4、https://pypi.org/project/pip/#files
   
代码:
from xml.etree import ElementTree as ET
import xlwt
# 提取元素数据
def parseXml():
    stringes = []  # 站点名称和代码列表
    # 打开文件、把strings.xml放到相同目录中
    dom = ET.parse('strings.xml')
    # 文档根元素
    root = dom.getroot()
    print("tag : " + root.tag)
    # 寻找String元素
    find_tag = root.findall(".//string")
    for p in find_tag:
        stringes.append(p.text)  # 加入列表
    print("根数目:" ,len(find_tag))
    print("得到元素数目: " ,len(stringes))
    return stringes
# 写入excel、把文本一列一列陈列
def save_excel(file_path, datas):
    f = xlwt.Workbook()
    sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)
    r = 0  # 行
    c = 0  # 列
    for data in datas:
        print("data :", data)
        sheet1.write(r, c, data)
        r = r + 1
    f.save(file_path)
if __name__ == '__main__':
    listdemo = parseXml()
    save_excel('输出文档.xls', listdemo)
    参考:
    
    https://www.freesion.com/article/4646450676/
   
 
版权声明:本文为weixin_38622593原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
