Home > Article > Backend Development > How to read and write excel in python

How to read and write excel in python?
Python reads the contents of the excel table
1 The first step is to take a look at the contents of the excel table. This table is placed in the root directory of the d drive , the path is d://1.xlsx, as shown in the figure below:

2 The second step is to enter the
“
import xlrd
datas = xlrd.open_workbook('d://1.xlsx')
table = datas.sheets()[0]
print(table.nrows)
print(table.ncols)
print(table.row_values(0))
print(table.col_values(0))
print(table.cell(0,0).value)
”code in python to proceed Read the contents of the excel table, as shown in the figure below:

3 The third step is to run the py file. You can see that the contents of the first row and column of the excel table have been read. , as shown in the figure below:

python writes the content into the excel table
The first step is to enter
“
import xlwt
wb = xlwt.Workbook(encoding='ascii')
ws = wb.add_sheet('wg')
ws.write(0, 0, label='hs')
ws.write(0, 1, label='wd')
ws.write(1, 0, label='你好啊')
wb.save('d://xt.xls')
”Code, write the content into the xt.xls table, as shown in the figure below:

2 In the second step, run the py file, and you can see that xt.xls is generated on the d drive When you open the form, you can see that the content has been written in, as shown below:

Push: "Python Tutorial"
The above is the detailed content of How to read and write excel in python. For more information, please follow other related articles on the PHP Chinese website!