查找列表中定义生成代码

简单修改即可使用
|  ![在这里插入图片描述](https://img-blog.csdnimg.cn/d8ac0760d9e64c36989738d62bf8fed4.png
|  |
|--|--|
|  |  |

#导入模块
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()
    
    
    





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Li_凌澜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值