【环境】
xlwt :1.3
python:3.9.5
【代码示例】
import os
import re
import xlwt
def robot_file_xls_utils():
current_path = os.path.dirname(__file__)
file_path = os.path.join(current_path,‘trader_elements.robot‘)
file = open(file_path,‘r‘ ,encoding=‘utf-8‘)
re_file = file.read()
result = re.findall(‘xpath=(\S+)‘,re_file) # 获取所有的定位值
result_NAME = re.findall(‘[\u2E80-\u9FFF]+‘, re_file) # 获取所有的name
lines_name = len(result_NAME)
lines_pocate = len(result)
workbook = xlwt.Workbook(encoding = ‘ascii‘)
worksheet = workbook.add_sheet(‘trader_mode‘)
worksheet.write_merge(0, 0, 0, 0, ‘元素变量名‘) # 合并单元格
# 数据特殊处理
for name in range(1,lines_name):
if name ==95:
worksheet.write(name, 1, ‘0‘)
else:
worksheet.write(name, 1, result_NAME[name-1] )
for locate in range(1, lines_pocate):
worksheet.write(locate, 3, result[locate])
workbook.save(‘trader_model.xls‘) # 保存文件
robot_file_xls_utils()
原文:https://www.cnblogs.com/ygod323/p/14823612.html