其他分享
首页 > 其他分享> > 用xlrd读取Excel,用xlwt写入Excel数据

用xlrd读取Excel,用xlwt写入Excel数据

作者:互联网

import xlrd, xlwt, re

def read_file():
    #读取数据
    book = xlrd.open_workbook('file_org.xlsx')
    sheet = book.sheet_by_index(0)  #打开第一个页面,一个Excel里面可能有多个页面,在左下角那里
    nrows = sheet.nrows  #行数
    ncols = sheet.ncols  #列数
    #写入数据
    workbook = xlwt.Workbook(encoding='utf-8')
    worksheet = workbook.add_sheet('预售预告数据')  #在Excel里面创建一个页面
    for j in range(0, int(nrows)):
        print(j, sheet.row_values(j))
        for i in range(11):
            if i == 1 and j != 0:
                worksheet.write(j, i, round(float(sheet.row_values(j)[i]), 2))
            else:
                worksheet.write(j, i, sheet.row_values(j)[i])
    workbook.save('file1.xlsx')  #生成文件

def write_file(sum, list):
    workbook = xlwt.Workbook(encoding='utf-8')
    worksheet = workbook.add_sheet('预售预告数据')
    for i in range(len(list)):
        if i == 0 and sum != 0:
            data = re.findall(r"_(\d{5,20})%401", list[i])[0]
            click_url = "https://detail.tmall.hk/hk/item.htm?id=" + data
            worksheet.write(sum, i, click_url)
        else:
            worksheet.write(sum, i, list[i])
    workbook.save('file1.xlsx')

if __name__ == '__main__':
    # list = ['http://s.click.taobao.com/t?e=m%3D2%26s%3D%2B%2B6q8auYBchw4vFB6t2Z2ueEDrYVVa64Dne87AjQPk9yINtkUhsv0GTehcPDlMx7GmSlKu4AZKCL4MfOiC6uuAAwQNP5NSRdcAewsTGLqMHiMOSyNGzZffAy%2Fay3dFHh7oVdeaxbXwGADBVza98Rv%2BxCD59ekcySSGw5jH%2FwVs4sBTc7Ajiw2TRloW3biZSvewMs64pmc8rGDmntuH4VtA%3D%3D&scm=1007.15348.115058.0_26257&pvid=a3434e4a-8d75-423f-84d3-3d18c22e4748&app_pvid=59590_11.132.118.129_543_1590444281206&ptl=floorId:26257;originalFloorId:26257;pvid:a3434e4a-8d75-423f-84d3-3d18c22e4748;app_pvid:59590_11.132.118.129_543_1590444281206&union_lens=lensId%3AMAPI%401590444281%40a3434e4a-8d75-423f-84d3-3d18c22e4748_616077343215%401', '328.0', 'saisamorn赛莎梦泰国乳胶内衣女无痕无钢圈小胸聚拢运动文胸美背', '满298.0减20.0', 'http://uland.taobao.com/coupon/edetail?e=Oh1ypC2C%2Fz0NfLV8niU3R5TgU2jJNKOfNNtsjZw%2F%2FoLxhqIWIo3VB6L3c4GXLB4UYa9Te2CUYQCIjP%2Fs4CfQCawbAntFj%2BXYEHpTAWQ%2BSRFp4exEq6u%2FYVjo2ynIQ6qWdXwt6S39nAykamxjBDUoNzdC6dt479FzyLmQ9wrzAJNn1Z%2BMophLvmso7tNexgeTqegMGS5Vu4RMDQVG07AK7A%3D%3D&&app_pvid=59590_11.132.118.129_543_1590444281206&ptl=floorId:26257;app_pvid:59590_11.132.118.129_543_1590444281206;tpp_pvid:a3434e4a-8d75-423f-84d3-3d18c22e4748&union_lens=lensId%3AMAPI%401590444281%40a3434e4a-8d75-423f-84d3-3d18c22e4748_616077343215%401', '398', '50', '50.0', '2020-06-01 01:00:00', 'saisamorn海外旗舰店', '否']
    # write_file(0, list)
    read_file()

  

标签:xlwt,sheet,list,Excel,write,pvid,worksheet,workbook,xlrd
来源: https://www.cnblogs.com/qiaoer1993/p/12972469.html