1. 程式人生 > >python excel 讀寫

python excel 讀寫

exce cti .sh clas nbsp port work use 行數據

excel 讀取

import openpyxl
import xlwt

data = xlrd.open_workbook("C:/Users/4YUJIAN/Desktop/優惠備案.xls")
table = data.sheets()[0]
nrows = table.nrows #行數
ncols = table.ncols #列數
for i in range(nrows):
    r = table.row_values(i)  # 某一行數據
    print(r)

excel 寫

def write07Excel(path, value, fileName):
    wb = openpyxl.Workbook()
    sheet = wb.active
    sheet.title = fileName
    for i in range(len(value)):
        for j in range(len(value[i])):
            sheet.cell(row=i + 1, column=j + 1, value=str(value[i][j]))
    wb.save(path)
    print("寫入數據成功!")


def write03Excel(path, value, fileName):
    wb = xlwt.Workbook()
    sheet = wb.add_sheet(fileName)
    for i in range(len(value)):
        for j in range(len(value[i])):
            v = str(value[i][j])
            sheet.write(i, j, v)
    wb.save(path)
    print("寫入數據成功!")

  數據傳入格式 :

[(1,2,3)]  # 行列
[[2,3,4]]

  

python excel 讀寫