Python (write operation -xlwt module excel)

A mounting module xlwt

  • pip install xlwt

Two, excel write operation

  • In this way only add or overwrite files are written
Import xlwt 

# Create a set coding workbook 
workbook = xlwt.Workbook (encoding = ' UTF-. 8 ' )
 # Create a Sheet 
Worksheet = workbook.add_sheet ( ' My the Worksheet ' )
 # write excel, write (row_index, col_index, value) parameters corresponding to the row, column, value 
worksheet.write (1,0, ' the this Test IS ' )
 # saved (after saving the new file in the directory xxx.xls) 
workbook.save ( ' D: \\ Excel_test.xls ' )

 

  • In this way modify the contents of the file will not overwrite the original file content will only modify the content specified 
# Coding. 8 = UTF- 

from xlutils.copy Import Copy
 Import to xlrd 

# open to modify excel 
Book xlrd.open_workbook = ( ' D: \\ Test.xls ' )
 # copy original editing excel 
new_book = Copy (Book)
 # selected to be modified by get_sheet (sheet_index) sheet page 
Sheet = new_book.get_sheet (0)
 # write to modify, write (row_index, col_index, value ) parameters corresponding to the row, column, value 
sheet.write (0,1, ' the Test ' )
 # save the new excel, excel must save extension is .xls, and .xlsx is not capable of 
new_book.save ( ' d: \\ Test.xls ' )

 

Guess you like

Origin www.cnblogs.com/Mr-ZY/p/11872971.html