1. 程式人生 > >python建立Excel檔案資料的方法

python建立Excel檔案資料的方法

# -*- coding: utf-8 -*-
# @Time : 2018/12/6 17:10
# @Author : suchao
# @Disc: : 生成10000條Excel資料
# @File : 1000data.py
# @Software: PyCharm
import xlrd ,xlwt
import random

"""建立一個excel物件"""
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
"""建立sheet"""
sheet = book.add_sheet('test',cell_overwrite_ok=True)
"""新增欄位"""
sheet.write(0, 0, '編號')
sheet.write(0, 1, '名稱')
sheet.write(0, 2, 'DISPLAY_X')
sheet.write(0, 3, 'DISPLAY_Y')
sheet.write(0, 4, '銷售數量')
sheet.write(0, 5, '銷售單價')
sheet.write(0, 6, '銷售收入')
sheet.write(0, 7, '單位成本')
sheet.write(0, 8, '銷售成本')
sheet.write(0, 9, '銷售毛利')

"""寫入編號欄位資料"""
for id in range(10000):
sheet.write(id + 1, 0, id)
sheet.write(id + 1, 1, "肯德基餐廳"+str(id))
sheet.write(id + 1, 2, round(random.uniform(3, 52), 2))
sheet.write(id + 1, 3, round(random.uniform(73, 135), 2))
sheet.write(id + 1, 4, round(random.uniform(1, 10001), 2))
sheet.write(id + 1, 5, round(random.uniform(1, 10001), 2))
sheet.write(id + 1, 6, round(random.uniform(1, 10001), 2))
sheet.write(id + 1, 7, round(random.uniform(1, 10001), 2))
sheet.write(id + 1, 8, round(random.uniform(1, 10001), 2))
sheet.write(id + 1, 9, round(random.uniform(1, 10001), 2))
book.save(r'.\chinaXY.xls')