专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Perl/Python

python处理excel隐藏单元格解决办法

发布时间:2011-06-29 20:11:13 文章来源:www.iduyao.cn 采编人员:星星草
python处理excel隐藏单元格
excel里面有的内容是隐藏的,现在想用python读取excel里面的值,隐藏的内容也都一并被读取出来了,有没什么办法和属性可以不读取到隐藏的单元格,请懂的帮忙下啊,谢谢了。。。在线等!

------解决方案--------------------
http://groups.google.com/group/python-excel/browse_thread/thread/de8838a452ac0cb

Python code

#!/usr/bin/env python
#encoding:utf-8

from xlrd import open_workbook,cellname

def main():
    #打开文件
    xlsfilename='c:/temp/workbook1.xls'
    book=open_workbook(xlsfilename,formatting_info=True)
    print 'book.nsheets:',book.nsheets

    #三种方式得到sheet对象
    #begin
    print 'begin get sheet objects:'
    #索引
    mysheets=[]
    for sheet_index in range(book.nsheets):
        sheet=book.sheet_by_index(sheet_index)
        mysheets.append(sheet)
        print sheet

    #名字
    print book.sheet_names()
    for sheet_name in book.sheet_names():
        print book.sheet_by_name(sheet_name)

    #对象集
    for sheet in book.sheets():
        print sheet
    #end

    cursheet=mysheets[0]

    cinfomap=cursheet.colinfo_map
    rinfomap=cursheet.rowinfo_map
    #print 'colinfo_map:',cinfomap
    #print 'rowinfo_map:',rinfomap
    print 'show row and column hidden attribute:'
    for key,value in cinfomap.items():
        print 'col %d hidden attribute:%d' % (key,value.hidden)
    for key,value in rinfomap.items():
        print 'col %d hidden attribute:%d' % (key,value.hidden)


    #内省一个表格,包括隐藏行列
    #begin
    print 'begin introspect a sheet(including all):'
    print 'name:',cursheet.name
    print 'nrows:',cursheet.nrows
    print 'ncols:',cursheet.ncols

    for row_index in range(cursheet.nrows):
        for col_index in range(cursheet.ncols):
            tcell=cursheet.cell(row_index,col_index)
            cname=cellname(row_index,col_index)
            cvalue=tcell.value
            print cname,'=',cvalue
    #end

    #内省一个表格,不包括隐藏行列
    #begin
    print 'begin introspect a sheet(not including hidden):'
    print 'name:',cursheet.name
    print 'nrows:',cursheet.nrows
    print 'ncols:',cursheet.ncols

    for row_index in range(cursheet.nrows):
        if rinfomap.get(row_index,0) and rinfomap[row_index].hidden==1:
            continue
        for col_index in range(cursheet.ncols):
            if cinfomap.get(col_index,0) and cinfomap[col_index].hidden==1:
                continue
            tcell=cursheet.cell(row_index,col_index)
            cname=cellname(row_index,col_index)
            cvalue=tcell.value
            print cname,'=',cvalue
    #end


if __name__ == '__main__':
    main()
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: