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

How to read and write excel in python

藏色散人
藏色散人Original
2019-09-30 11:02:515567browse

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:

How to read and write excel in python

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:

How to read and write excel in python

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:

How to read and write excel in python

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:

How to read and write excel in python

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:

How to read and write excel in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn