美文网首页
Python操作Excel

Python操作Excel

作者: 袁先生的笔记 | 来源:发表于2022-08-03 16:32 被阅读0次
import xlrd

excel_file = 'xxx.xls'
book = xlrd.open_workbook(excel_file)
sheet_name_list = book.sheet_names()

for sheet_name in sheet_name_list:
    print('reading sheet:  ' + sheet_name)
    sheet = book.sheet_by_name(sheet_name)

    header_list = sheet.row_values(0, start_colx=0, end_colx=None)
    print('header_list: ' + str(header_list))

    rows_number = sheet.nrows
    print('rows number: ' + str(rows_number))
    cols_number = sheet.ncols
    print('columns number: ' + str(cols_number))

    row_index = 0
    while row_index < rows_number:
        col_index = 0
        while col_index < cols_number:
            value = sheet.cell_value(row_index, col_index)
            print(row_index, col_index, value)
            col_index = col_index + 1
        row_index = row_index + 1
import xlwt

excel_list = [['a', 'b', 'c'], [1, 2, 3]]

file = 'xxx.xls'
book = xlwt.Workbook(encoding= 'ascii')
sheet = book.add_sheet('xxx')

for row_index in range(len(excel_list)):
    for col_index in range(len(excel_list[row_index])):
        sheet.write(row_index, col_index, excel_list[row_index][col_index])

book.save(file)

相关文章

网友评论

      本文标题:Python操作Excel

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