1. 程式人生 > 實用技巧 >使用python的xlrd模組讀取excel內容

使用python的xlrd模組讀取excel內容

基礎操作

Excel的內容如下

程式碼

import xlrd as x

local_filepath = r"D:\Python\Students.xlsx"

# Open work book, no need to close it
wb = x.open_workbook(local_filepath)

# Open work sheet
ws = wb.sheet_by_index(0)

# sheet_by_name
# ws = wb.sheet_by_name('Sheet1')

# Get row count and column count
print("Work sheet row count: {0}, column count: {1}
".format(ws.nrows, ws.ncols)) # Get cell value print("Get Cell(0, 0) value:", ws.cell_value(0, 0)) # Get row values for i in range(ws.nrows): print(ws.row_values(i)) # Get rows with generator ws.get_rows() # Get column values for i in range(ws.ncols): print(ws.col_values(i)) ###############################################################
# Cell is empty emptycell = ws.cell_value(2, 1) emptycell == '' # Output True # Cell is date from datetime import datetime from xlrd import xldate_as_tuple date = datetime(*xldate_as_tuple(ws.cell_value(2, 3), 0)) datecell = date.strftime('%m/%d/%y %H:%M') print(datecell)

輸出