简单修改即可使用
#导入模块
import os
import openpyxl
import time
def fun1():
#打开需生成的.c
with open('C:/Users/CQ-rjb-lww/Desktop/a/a.c' , 'w') as f_c:
wb = openpyxl.load_workbook("C:/Users/CQ-rjb-lww/Desktop/a/1.xlsx")
sheet = wb['Sheet1'] #指定表格
f_c.write("//data: " + time.asctime() ) #添加时间搓
f_c.write("\n\n")
f_c.write("void GpioInit( void)")
f_c.write("\n{ \n")
f_c.write(" GPIO_Reset(GPIOx); \n")
f_c.write(" GPIO_InitTypeDef GPIO_InitStructure; \n")
f_c.write(" GPIO_Struct_Init(&GPIO_InitStructure); \n")
for rowNum in range( 2 , sheet.max_row + 1): #循环
value1 = sheet.cell(rowNum ,1).value #读取第一列 第n行数据
f_c.write(" //init gpio" +value1 )
f_c.write("\n")
f_c.write(" GPIO_InitStructure.m_Pin = " +value1 )
f_c.write("; \n")
f_c.write(" GPIO_InitStructure.m_Speed = " + sheet.cell(rowNum ,2).value)
f_c.write("; \n")
f_c.write(" GPIO_InitStructure.m_Mode = " + sheet.cell(rowNum ,3).value)
f_c.write("; \n")
f_c.write(" GPIO_InitStructure.m_PullUp = " + sheet.cell(rowNum ,4).value)
f_c.write("; \n")
f_c.write(" GPIO_InitStructure.m_PullDown = " + sheet.cell(rowNum ,5).value)
f_c.write("; \n")
f_c.write("\n")
bit_set = sheet.cell(rowNum ,6).value
if value1 == "GPIO_1": #判断gpio组
f_c.write(" GPIO_Configuration(GPIOA,&GPIO_InitStructure); \n")
if bit_set != "NULL" :
f_c.write(" GPIO_Set_Output_Data_Bits(GPIOA, "+value1+", "+ bit_set +" ); \n")
else:
f_c.write(" GPIO_Configuration(GPIOB,&GPIO_InitStructure); \n")
if bit_set != "NULL" :
f_c.write(" GPIO_Set_Output_Data_Bits(GPIOB, "+value1+", "+ bit_set +" ); \n")
f_c.write("\n")
f_c.write("}")
f_c.close()#关闭文件
myname=input("修改成功 任意键退出:")
def main():
fun1()
if __name__=='__main__':
print("main runing...")
main()