美文网首页
python操作excel

python操作excel

作者: shu_ke | 来源:发表于2018-06-23 15:48 被阅读56次

    python操作excel

    读取excel

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import xlrd
    from xlrd.book import Book
    from xlrd.sheet import Sheet
    from xlrd.sheet import Cell
    
    workbook = xlrd.open_workbook('基础课程大纲.xlsx')
    
    sheet_names = workbook.sheet_names()
    
    # sheet = workbook.sheet_by_name('工作表1')
    sheet = workbook.sheet_by_index(1)
    
    # 循环Excel文件的所有行
    for row in sheet.get_rows():
        # 循环一行的所有列
        for col in row:
            # 获取一个单元格中的值
            print(col.value)
    

    写excel

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import xlwt
    
    wb = xlwt.Workbook()
    sheet = wb.add_sheet('sheet1')
    
    for row in range(10):
        for col in range(5):
            sheet.write(row, col, '第{0}行第{1}列'.format(row, col))
    
    wb.save('xxx.xls')
    
    
    # 更多示例:https://github.com/python-excel/xlwt/tree/master/examples
    

    相关文章

      网友评论

          本文标题:python操作excel

          本文链接:https://www.haomeiwen.com/subject/rltdyftx.html