#coding:utf8 from loguru import logger import json, re, os from xlrd import open_workbook from xlutils.copy import copy false = False true = True null = None import xlwt def write_excel_xls(): # 保存到当前工程目录 path = '淘系商城活动商品筛查.xls' sheet_name = 'xls格式表' if os.path.exists(path): logger.info('excel exited') value = ["40371355", "https://detail.tmall.com/item.htm?id=646058037286", "是", "跨店每199减25"] rexcel = open_workbook(path) # 用wlrd提供的方法读取一个excel文件 rows = rexcel.sheets()[0].nrows # 用wlrd提供的方法获得现在已有的行数 logger.info('had rows: {}'.format(rows)) excel = copy(rexcel) # 用xlutils提供的copy方法将xlrd的对象转化为xlwt的对象 table = excel.get_sheet(0) # 用xlwt对象的方法获得要操作的sheet table.write(rows, 0, value[0]) # xlwt对象的写方法,参数分别是行、列、值 table.write(rows, 1, value[1]) # xlwt对象的写方法,参数分别是行、列、值 table.write(rows, 2, value[2]) # xlwt对象的写方法,参数分别是行、列、值 table.write(rows, 3, value[3]) # xlwt对象的写方法,参数分别是行、列、值 excel.save(path) # xlwt对象的保存方法,这时便覆盖掉了原来的excel else: logger.info('excel not exited') value_title = ['文章id', '商品url', '是否是跨店满减', '描述'] workbook = xlwt.Workbook() # 新建一个工作簿 sheet = workbook.add_sheet(sheet_name) # 在工作簿中新建一个表格 for j in range(0, len(value_title)): sheet.write(0, j, value_title[j]) # 像表格中写入数据(对应的行和列) workbook.save(path) # 保存工作簿 logger.info('excel head writeed')
def main(): write_excel_xls() if __name__ == '__main__': main()