White learn Python (23): Excel Basic Operation (on)

Life is short, I chose Python

The foregoing Portal

White learn Python (1): Opening

White Science Python (2): basic data type (on)

White Science Python (3): fundamental data types (lower)

White Science Python (4): Variable Basic Operation

White Science Python (5): base operator (on)

White Science Python (6): base operator (lower)

White Science Python (7): based flow control (on)

White Science Python (8): the basis of flow control (lower)

White Science Python (9): basic data structure (list) (a)

White Science Python (10): basic data structure (list) (lower)

White Science Python (11): basic data structure (tuples)

White Science Python (12): basic data structure (dictionary) (a)

White Science Python (13): basic data structure (dictionary) (lower)

White Science Python (14): basic data structure (set) (a)

White Science Python (15): basic data structure (set) (lower)

White Science Python (16): the basic data type (function) (a)

White Science Python (17): the basic data type (function) (lower)

White learn Python (18): basic file operations

White learn Python (18): basic file operations

White learn Python (19): base Exception Handling

White learn Python (20): iterator basis

White Science Python (21): base generator

White Science Python (22): time and simple to use calendar module

Read Excel

I do not know the students have not seen the ads about Python operations of Excel. Xiao Bian is seen many times, and also be able to learn just ¥ 9.9, this small series of free introductory tutorial to bring Python operations Excel for everyone.

Look carefully, carefully study paper value of ¥ 9.9 Oh ~ ~ ~

This Niubi blown myself a little blush.

If it wants to operate Excel, then we need to create a Excel.

First create three Sheet in Excel, do not tell me she does not know what is Sheet, you may not be suitable for watching In this part.

Excel has been uploaded to the test using code repository, needy students can access.

Excel reads as follows:

Please ignore the lower right corner of the date and time, date and time it is not practical effect, only for demo use.

This is a Sheet data, there are three similar Sheet.

Fellow students may be seen later this Excel was able to guess a small series to be doing, yes, yes, you guessed it, small series is to count the average.

So that comes with Excel formula to calculate average scores are not fragrant it, it has to be considered in Python, waste of resources.

Excel built-in function is indeed powerful, not against this, but if I want to cross-Sheet operating it? To play this article is to show the operation to calculate the average score across Sheet.

First of all, we need to import third-party modules xlrd, because a third-party module, students are not installed you need to install. At the command line, type:

pip install xlrd

Wait for the progress bar has completed just fine.

Gets workbook:

First, we need to open Excel we just created in the code:

workbook = xlrd.open_workbook('test.xlsx')

Xiao Bian here for convenience, test.xlsxand the code in the same directory, the problem should pay attention to the actual path of the file operation, either using a relative path or absolute path, should write right.

Sheet on the operation:

There are many operations related to Sheet, we list a few common:

# 输出所有 sheet 的名字
print(workbook.sheet_names())
# 获取所有的 sheet
print(workbook.sheets())
# 根据索引获取 sheet
print(workbook.sheet_by_index(1))
# 根据名字获取 sheet
print(workbook.sheet_by_name('1班'))

I'm not here to demonstrate the output results, and operating under their own fellow students know :)

Common operations:

Gets the number of rows and columns:

sheet1 = workbook.sheets()[0]
# 获取行数
print(sheet1.nrows)
# 获取列数
print(sheet1.ncols)

The results are as follows:

6
4

And acquiring entire row entire row of data (data type list):

# 获取第 2 行内容
print(sheet1.row_values(1))
# 获取第 3 列内容
print(sheet1.col_values(2))

The results are as follows:

['小明', 76.0, 85.0, 95.0, '']
['数学', 85.0, 58.0, 96.0, '', '']

Obtaining cell data:

cell1 = sheet1.cell(1, 1).value
# 行索引
cell2 = sheet1.row(1)[1].value
cell3 = sheet1.cell(1, 2).value
# 列索引
cell4 = sheet1.col(2)[1].value

The results are as follows:

76.0 76.0 85.0 85.0

Gets the date data type:

date_value = xlrd.xldate_as_datetime(sheet1.cell_value(5, 3), workbook.datemode)
print(type(date_value), date_value)

The results are as follows:

<class 'datetime.datetime'> 2019-11-07 20:49:05

Here is turned into data directly by a process datetimetype, xlrdalso may provide data into a tuple, the tuple then converted to date.

date_tulp = xlrd.xldate_as_tuple(sheet1.cell_value(5, 3), workbook.datemode)
print(type(date_tulp), date_tulp)
year, month, day, hour, minute, second = date_tulp
print(datetime.datetime(year, month, day, hour, minute, second))

The results are as follows:

<class 'tuple'> (2019, 11, 7, 20, 49, 5)
2019-11-07 20:49:05

Today's main event is the averaging, but I do not want to paste the code in the article, had to please the students start their move slightly ~ ~ ~

Of course, if it can not handle access code repository, small or specific implementation code will be submitted to the code repository, but small or hope that the students able to complete the job ~ ~

Sample Code

This series of small series all the code will be placed on code management repository Github and Gitee, to facilitate access.

Sample Code -Github

Sample Code -Gitee

Guess you like

Origin www.cnblogs.com/babycomeon/p/11880144.html