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')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM