Search This Blog

Oct 11, 2012

Access excel file by Python

    I have a task to extract register data from SOC spec and fill in excel file. I chosen Python script language to do this automatic job. I have found two 3rd party tools xlwt and openpyxl.
    I have tried xlwt, it has well documentation and easier to operation. However, it has two major weakness, first, it only support up to excel 2003; second, it doesn't support Python 3 which is default version in my group. So I turn to Openpyxl, before that, I would like record some information for xlwt which might be useful for other who don't care about those two weakness I mentioned above.

http://www.python-excel.org/

1. xlrd
* http://pypi.python.org/pypi/xlrd
* Version 0.8.0
* c:\python26\python.exe setup.py install

2. xlwt
* http://pypi.python.org/pypi/xlwt
* version 0.7.4
* c:\python26\python.exe setup.py install

3. Sample code
These same code can generate all style I needed in my task.
from xlwt import Workbook, easyxf
from xlwt.Utils import rowcol_to_cell

row0_style = easyxf(
    'font: name Calibri, bold True, height 220;'
    )
reg_row_style = easyxf(
    'pattern: pattern solid, fore_colour yellow;'
    'font: name Calibri, height 220;'
    )
common_style = easyxf(
    'font: name Calibri, height 220;'
    )

row0_data = [
    'Module',
    'Register',
    'Offset',
    'Bit Num',
    'Bit',
    'Bit Access',
    'Bit Field Reset',
    'Enum Name',
    'Enum Value',
    'Special',
    'Short Name',
    'Description',
    ]

w = Workbook()
#sheet1 = w.add_sheet('version')

sheet2= w.add_sheet('Base_alias')
sheet2.panes_frozen = True
sheet2.remove_splits = True
sheet2.horz_split_pos = 1
for col in range(12):
    sheet2.write(0,col,row0_data[col],row0_style)
for col in range(12):
    sheet2.write(1,col,rowcol_to_cell(1,col),reg_row_style)
for col in range(12):
    for row in range(2,80):
        sheet2.write(row,col,rowcol_to_cell(row,col),common_style)
w.save('panes.xls')

No comments: