python之openpyxl模块的使用

首先导入需要用到的库

import openpyxl
import re

然后将需要处理的excel表格以“读”的形式存在一个变量

file_name = r"test.xlsx"

然后创建一个文件名,这个文件是用来输出打印信息,生成需要的代码

file_o = open("test.sv", "w")

使用load_workbook将excel表格存入workbook变量

workbook = openpyxl.load_workbook(file_name)

使用get_sheet_by_name函数将需要处理的sheet存入sheet变量

sheet = workbook.get_sheet_by_name('Sheet1')

获取到行数rows和列数cols

rows = sheet.max_row
cols = sheet.max_column

使用cell(row=i, column=j).value获取对应表格的内容

ball_name   = sheet.cell(row=4, column=5).value

如果其中有需要替换内容中的某些字段,可以使用re库的sub函数,示例:将0x替换为32‘h,方便systemverilog识别。

ball_name  = re.sub("0x", "32'h", ball_name)

先写这么多了,以后用到更多功能后再补充。谢谢阅读。

贴上全部代码:

file_o = open("test.sv", "w")
file_name = "test.xlsx"
workbook = openpyxl.load_workbook(file_name)
sheet = workbook.get_sheet_by_name('Sheet1')
rows = sheet.max_row
cols = sheet.max_column
print("bit [31:0] addr;", file=file_o)
print("bit [31:0] data;", file=file_o)
#begin second row in sheet
    ball_name   = sheet.cell(row=i, column=5).value
    func1       = sheet.cell(row=i, column=16).value
    func2       = sheet.cell(row=i, column=18).value
    func3       = sheet.cell(row=i, column=20).value
    func4       = sheet.cell(row=i, column=22).value
    reg_offset  = sheet.cell(row=i, column=31).value
    #replace 0x to 32'h
    reg_offset  = re.sub("0x", "32'h", reg_offset)
    #add postfix _sel behind ball_name
    task_name   = ball_name + "_sel"
    print("task " + task_name + "(bit [1:0] func);", file=file_o)
    print("\tbegin", file=file_o)
    print("\t\tgpio_addr = `GPIO_BASE_ADDR + "+reg_offset+";", file=file_o)
    print("\t\tcase(func)", file=file_o)
    print("\t\t\t2'b00: data = ;", file=file_o)
    print("\t\t\t2'b01: data = ;", file=file_o)
    print("\t\t\t2'b10: data = ;", file=file_o)
    print("\t\t\t2'b11: data = ;", file=file_o)
    print("\t\t\tdefault: data = 0", file=file_o)
    print("\t\tendcase", file=file_o)
    print("\t\tp_m0.write(addr, data);", file=file_o)
    print("\t\t`uvm_info(get_type_name(), $sformatf(\"addr = 32'h%0h, data = 32'h%0h\", addr, data), UVM_LOW)", file=file_o)
    print("\tend", file=file_o)
    print("endtask:"+task_name, file=file_o)
    print("", file=file_o)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值