[python]Edit excel by xlrd and xlwt

2021-8-26 写技术

import xlrd
import xlwt

def edit_excel(fromFile, fromSheet):
    w_workbook = xlwt.Workbook()
    w_sheet = w_workbook.add_sheet("nicholas", cell_overwrite_ok=True)


    workbook = xlrd.open_workbook(fromFile)
    print(workbook.sheet_names())

    sheet = workbook.sheet_names()[fromSheet]
    sheet = workbook.sheet_by_index(fromSheet)

    print(sheet)
    row = sheet.nrows
    col = sheet.ncols
    print(row, col)

    for i in range(0, row):
        for j in range(0, col):
            v = sheet.cell(i, j).value
            w_sheet.write(i, j, v)
            print(v," ", end="")
        print(" ")
        w_sheet.write(i,5, "write")
        w_sheet.write(i,6, "log.anycle.com")


    w_workbook.save("nicholas.xls")


edit_excel("nicholas.xls", 0)

标签: python

发表评论:

Powered by anycle 湘ICP备15001973号-1