MTK intelligent platform battery curve is automatically generated

It is a very headache to change the battery curve. There are hundreds of data to be filled in for the four temperatures. It is too inefficient to type one by one, so write a script to automatically generate these data. I hope someone can use it. At the beginning, I planned to use MFC to write, but I needed to create a project and have cumbersome settings, so it would definitely take a lot of effort to use MFC. MATLAB is the second method I thought of. MATLAB can directly read excel files, and the data is stored in the matrix. It is very convenient to directly operate on the row and column data. However, MATLAB is not installed on the company's computer, so I have to give up this option. At this time, I thought of Python. Although it is not as convenient as matlab, it is much simpler than MFC, but it needs to install the xlrd module, which can be downloaded at https://pypi.python.org/pypi/xlrd. Not much to say, let’s learn my code below (the numbers inside are modified according to the specific content of the excel table)

# -*- coding: utf-8 -*-
# 说明:本文件是为了节省再MTK平台该电池曲线而设计的,因为人工一个一个数据去改太耗时间
# 使用方法
# 在main函数中调用函数 excel_table_byclone(file, col1, col2, row1, row2, arr_name, by_index)即可
# 该函数会生成$(arr_name).txt文件,内容为arr_name的数组
# 参数说明:
# file 要打开的excel文件名
# col1 第一列,因为数组里面是表格的两列数据
# col2 第二列
# row1 起始行,从0开始计数
# row2 结束行
# arr_name 要生成的数组名
# by_index 指定excel表格是哪个sheet,因为一个表格可以有多个sheet,这里我们一般为0


import  xdrlib ,sys
import xlrd


def open_excel(file= 'test.xls'):
try:
data = xlrd.open_workbook(file)
return data
except Exception,e:
print str(e)


# 读取指定列
def excel_table_byclone(file= 'test.xls',col1=1,col2=1,row1=1,row2=1,arr_name = 'desarr', by_index=0):
#str = 'cust_battery_meter_table.h' # "%s.txt"%(arr_name)
str = "%s.txt"%(arr_name)
pf = open(str, 'w')
data = open_excel(file)
table = data.sheets()[by_index]
str = "%s[] = { \n"%(arr_name)
pf.write(str)
pf.write('')


for rownum in range(row1,row2):
row = table.row_values(rownum)
if row:
str = "\t{%3d,\t%3d},\n"%(row[col1]+0.5, row[col2]+0.5)
# pf.seek(-1)
pf.write(str)
print(str)
str = "};\n"
pf.write(str)
pf.close()

def main():
#excel_table_byclone('a11.xls', 2,3,2,95)
# 电压电量
excel_table_byclone('a11.xls', 5,1,1,76,'battery_profile_t50')
excel_table_byclone('a11.xls', 12,8,1,76,'battery_profile_t25')
excel_table_byclone('a11.xls', 19,15,1,76,'battery_profile_t00')
excel_table_byclone('a11.xls', 26,22,1,76,'battery_profile_t10')

# 电压电阻
excel_table_byclone('a11.xls', 6,1,1,76,'battery_profile_r50')
excel_table_byclone('a11.xls', 13,8,1,76,'battery_profile_r25')
excel_table_byclone('a11.xls', 20,15,1,76,'battery_profile_r00')
excel_table_byclone('a11.xls', 27,22,1,76,'battery_profile_r10')

# if __name__=="__main__":
main()
 



Guess you like

Origin blog.csdn.net/bira55/article/details/48634105