Python进阶--Python操作excel

一、xlsxwirter三方库

优点:
写的快
功能多

缺点:
只能写
问题:
内容并不是直接写入文件中
只有在关闭的时候才会写入文件

实例:

# 创建excel文件
wb = xlsxwriter.Workbook("cars.xlsx")
# 在该文件下创建sheet
ws = self.wb.get_worksheet_by_name("Car") or self.wb.add_worksheet("Car")
ws.write(row,col,*arg)
wb.close()

二、openpyxl库

可读可写可修改

使用:

        try:
            wb = load_workbook(filename)
        except Exception as e:
            wb = Workbook(filename)

        # 获取worksheet
        try:
            ws = wb.get_sheet_by_name(car_brand_name)
        except Exception as e:
            ws = wb.create_sheet(car_brand_name)

        # 写入数据
        ws.append((item.get("car_name"),item.get("car_unit"),item.get("car_price"),item.get("car_price_buy")))

        wb.save(filename)

你可能感兴趣的:(python,excel,数据库)