This article mainly introduces Python xlrd reading Excel date Type 2 methods, this article also explained xlrd reading Excel one cell method, needs the friend to be possible to refer to under
There is a excle form that needs to be filtered and written to the database, but the date type cell is a number, and the solution is queried.
Basic code structure
The code is as follows:
data = Xlrd.open_workbook (Excel_path)
Table = Data.sheet_by_index (0)
lines = Table.nrows
cols = Table.ncols
Print U ' The total is%s, cols is%s '% (lines, cols)
To read a cell:
The code is as follows:
Table.cell (x, y). Value
X: Line
Y: Columns
Rows, columns are all starting from 0
* Time type conversion, turn Excel time into Python time (two ways)
Excel a cell 2014/7/8
The code is as follows:
Xlrd.xldate_as_tuple (Table.cell (2,2). Value, 0) #转化为元组形式
(2014, 7, 8, 0, 0, 0)
Xlrd.xldate.xldate_as_datetime (Table.cell (2,2). Value, 1) #直接转化为datetime对象
Datetime.datetime (2018, 7, 9, 0, 0)
Table.cell (2,2). Value #没有转化
41828.0
Source view:
The code is as follows:
# @param xldate the Excel number
# @param Datemode 0:1900-based, 1:1904-based.
Xldate_as_tuple (Xldate, Datemode)
Entering a cell of a date type returns a tuple of time structures that can be composed of time types based on that tuple
Datemode has 2 options basically we all use the 1900 based timestamp
The code is as follows:
##
# Convert an Excel date/time number into a Datetime.datetime object.
#
# @param xldate the Excel number
# @param Datemode 0:1900-based, 1:1904-based.
#
# @return a datetime.datetime () object.
#
def xldate_as_datetime (Xldate, Datemode)
The input parameter is the same as above, but the return value is a datetime type and you do not need to convert the
Of course, these two functions have the corresponding inverse function, the Python type into the corresponding excle time type.