Python使用xlrd和xlwt讀取和寫入excel詳細教程


安裝

pip install xlrd
pip install xlwt
pip install xlutils

 

導入

import xlrd
import xlwt
import xlutils

 

讀取excel

# ----讀取excel----
data= xlrd.open_workbook(excel_file)

 

讀取sheet

# ----讀取sheet----
# 通過索引順序獲取sheet 
table = data.sheets()[0]

# 通過索引順序獲取sheet 
table = data.sheet_by_index(0))

# 通過名稱獲取sheet 
table = data.sheet_by_name("sheet")

# 返回book中所有sheet的名字
names = data.sheet_names()

# 傳入索引或sheet名檢查某個sheet是否導入完畢
table.sheet_loaded("sheet")
table.sheet_loaded(0)

# sheet名
table.name

# sheet列數
table.ncols

# sheet行數
table.nrows

 

讀取sheet的行

# 返回由rowx行中所有的單元格對象組成的列表
table.row(rowx)

# 獲取rowx行第一個單元格的類型
# 0. empty(空的),1 string(text), 2 number, 3 date, 4 boolean, 5 error, 6 blank(空白表格)
table.row(rowx)[0].ctype

# 獲取rowx行第一個單元格的值
table.row(rowx)[0].value

# 返回由rowx行中所有的單元格對象組成的列表
table.row_slice(self, rowx, start_colx=0, end_colx=None)

# 返回由rowx行中所有單元格的數據類型組成的列表
table.row_types(rowx, start_colx=0, end_colx=None)

# 返回由rowx行中所有單元格的數據組成的列表
table.row_values(rowx, start_colx=0, end_colx=None)

# 返回rowx行的有效單元格長度
table.row_len(rowx)

 

讀取sheet的列

#返回colx列中所有的單元格對象組成的列表
table.col(colx, start_rowx=0, end_rowx=None)  

#返回colx列中所有的單元格對象組成的列表
table.col_slice(colx, start_rowx=0, end_rowx=None)  

#返回colx列中所有單元格的數據類型組成的列表
table.col_types(colx, start_rowx=0, end_rowx=None) 
   
#返回colx列中所有單元格的數據組成的列表
table.col_values(colx, start_rowx=0, end_rowx=None)   

 

讀取sheet的單元格

# 返回單元格對象
cell = table.cell(rowx,colx)
# 單元格數據類型
# 0. empty(空的),1 string(text), 2 number, 3 date, 4 boolean, 5 error, 6 blank(空白表格)
cell.ctype
# 單元格值
cell.value
# 返回單元格中的數據類型
table.cell_type(rowx,colx)
# 返回單元格中的數據
table.cell_value(rowx,colx)
# 暫時還沒有搞懂
table.cell_xf_index(rowx, colx)

 

寫入excel

# 使用xlutils將xlrd讀取的對象轉為xlwt可操作對象,table即上述xlrd讀取的table
workbook = xlutils.copy(table)

# 或者如果你只是想創建一張空表
workbook = xlwt.Workbook(encoding = 'utf-8')

# 創建一個sheet
worksheet = workbook.add_sheet('sheet')
# 獲取一個已存在的sheet
worksheet = workbook.get_sheet('sheet')

# 寫入一個值,括號內分別為行數、列數、內容
worksheet.write(row, column, "memeda")

workbook.save('memeda.xls')

 

帶樣式寫入示例

workbook = xlwt.Workbook(encoding = 'utf-8')
style = xlwt.XFStyle()
font = xlwt.Font() # 創建字體
font.name = 'Arial'
font.bold = True # 黑體
font.underline = True # 下划線
font.italic = True # 斜體字
font.colour_index = 2 # 顏色為紅色
style.font = font
worksheet.write(row, column, "memeda", style)
workbook.save('memeda.xls')

 

輸出多種顏色字體

import xlwt

workbook = xlwt.Workbook(encoding='utf-8')


def get_style(i):
    style = xlwt.XFStyle()
    font = xlwt.Font()  # 創建字體
    font.colour_index = i
    style.font = font
    return style


sheet = workbook.add_sheet("memeda")
for i in range(0, 100):
    sheet.write(i, 0, "memeda", get_style(i))
workbook.save('memeda.xls')

 

import xlrd
import os


file = os.getcwd()+'\\pp.xls'
def read_excel():

    wb = xlrd.open_workbook(filename=file)#打開文件
    # 通過碎銀獲取sheel
    table = wb.sheets()[0]
    print(table)
    table = wb.sheets()[1]
    print(table)
    # 通過索引順序獲取sheet
    table = wb.sheet_by_index(0)
    print(table.name)
    print(table.ncols)
    print(table.read)
    print(table)
    table = wb.sheet_by_index(1)
    print(table)
    # 通過名稱獲取sheet
    table = wb.sheet_by_name("Sheet1")
    print(table)
    table = wb.sheet_by_name("Sheet2")
    print(table.name)
    # 返回book中所有sheet的名字
    table = wb.sheet_names()
    print(table)
    # 傳入索引或sheet名檢查某個sheet是否導入完畢
    ta = wb.sheet_loaded("Sheet1")
    print(ta)
    ta = wb.sheet_loaded(0)
    print(ta)
    table = wb.sheet_by_index(0)
    rowx = 0
    # 返回由rowx行中所有的單元格對象組成的列表

    print(table.row(rowx))
    # 獲取rowx行第一個單元格的類型
    # 0. empty(空的),1 string(text), 2 number, 3 date, 4 boolean, 5 error, 6 blank(空白表格)
    print(table.row(rowx)[0].ctype)
    # 獲取rowx行第一個單元格的值
    print(table.row(rowx)[0].value)
    # 返回由rowx行中所有的單元格對象組成的列表
    print(table.row_slice(rowx, start_colx=0, end_colx=None))
    # 返回由rowx行中所有單元格的數據類型組成的列表
    print(table.row_types(rowx, start_colx=0, end_colx=None))
    # 返回由rowx行中所有單元格的數據組成的列表
    print(table.row_values(rowx, start_colx=0, end_colx=None))
    # 返回rowx行的有效單元格長度
    print(table.row_len(rowx))
    # 返回colx列中所有的單元格對象組成的列表
    colx = 0
    print(table.col(colx, start_rowx=0, end_rowx=None))
    # 返回colx列中所有的單元格對象組成的列表
    print(table.col_slice(colx, start_rowx=0, end_rowx=None))
    # 返回colx列中所有單元格的數據類型組成的列表
    print(table.col_types(colx, start_rowx=0, end_rowx=None))
    # 返回colx列中所有單元格的數據組成的列表
    print(table.col_values(colx, start_rowx=0, end_rowx=None))
    # 讀取sheet的單元格
    # 返回單元格對象
    print(table.cell(rowx, colx))
    # 單元格數據類型
    # 0. empty(空的),1 string(text), 2 number, 3 date, 4 boolean, 5 error, 6 blank(空白表格)
    print(table.cell(rowx, colx).ctype)
    #  返回單元格中的數據類型
    print(table.cell_type(rowx, colx))
    #  返回單元格中的數據
    print(int(table.cell_value(rowx+5, colx+4)))
    #  返回單元格中的數據



if __name__ == '__main__':

    read_excel()

 

 


免責聲明!

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



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