不为别的,只为做一个连自己都羡慕的人

python读写excel

import xlrd
import trans
from xlutils.copy import copy


def readExcel():
    rea = xlrd.open_workbook('union_group.xls', formatting_info=True)
    print(rea)
    she = rea.sheets()[0]
    nrows = she.nrows
    print('表格总行数', nrows)
    for key in range(1, nrows):
        row3_values = she.row_values(key)
        print(row3_values)
        trans_res = trans.translation("zh", row3_values[0])
        if trans_res["code"] == 0:
            translation = trans_res["translation"]
        else:
            translation = ""
        print(key)
        aa = writeExcle(key, 1, translation)

        print(aa)
        # break

def writeExcle(row, col, translation):
    rb = xlrd.open_workbook('union_group.xls', formatting_info=True)
    # r_sheet = rb.sheet_by_index(0)
    wb = copy(rb)
    sheet = wb.get_sheet(0)
    sheet.write(row, col, translation)
    wb.save('union_group.xls')
    return "成功"


if __name__ == '__main__':
    readExcel()
trans是用来做翻译的,不许要的话可以注销
posted @ 2021-12-03 12:20  升级打怪  阅读(29)  评论(0编辑  收藏  举报