Python As a powerful data processing tool , Here is a brief introduction python Read write excel Tabular data .

<> one ,python write in excel form

Import xlwt modular
Generate an empty excel form , Named my_test.xls
import xlwt # Create Workbook f = xlwt.Workbook() # Create a sheet sheet1 = f.add_sheet(
'my_test', cell_overwrite_ok=True) # Save file"my_test.xls")
Generate an empty excel The table is as follows :

Click to use wps Open it and see the contents below :
Empty content .

Now write some data .
import xlwt # Create Workbook f = xlwt.Workbook() # Create a sheet sheet = f.add_sheet(
'my_test', cell_overwrite_ok=True) # Define the size of each column col1 = sheet.col(0) col1.width =
256 * 10# size 256x10 col2 = sheet.col(1) col2.width = 256 * 20# size 256x20 #
Initialize the first row as the column name row0 = [' full name ', ' achievement '] # Write first row column name for i in range(0, len(row0)): sheet.
write(0, i, row0[i])# The first 0 Line number i column # Similarly, the remaining data is written in the above way data=[[' Xiao Ming ',89], [' petty thief ',91], [
' Xiao Wang ',77], [' Little fat ',69]] for i in range(len(data)): for j in range(len(data[i])):
# Because No 0 Behavior listing , So here's the subscript i+1 sheet.write(i+1,j,data[i][j])# The first i+1 Line number j Column write data data[i][j] # Save file"my_test.xls")
Use again wps Open the table as follows

Pay attention to the table data and the size of each column of the table .

Write complete .

<> two ,python read excel form

Import xlrd modular
import xlrd # open excel read file data = xlrd.open_workbook('my_test.xls') #
according to sheet Subscript select read content sheet = data.sheet_by_index(0)# A table has multiple pages , Subscript from 0 start , We choose No 0 Just the page #
Gets the total number of rows in the table nrows = sheet.nrows for i in range(nrows): print(sheet.row_values(i))
The printed content is as follows