程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python uses xlwt, xlrt, xlutils to read and write existing excel methods in XLS format

編輯:Python

1、 Third party libraries used

        xlwt、xlrt、xlutils

2、 Use xlrd Open the original form

open_workbook The first parameter is the file path , The function of the second parameter is to retain the original format of the file

workbook=xlrd.open_workbook('user.xls',formatting_info=True)

3、 Use xlutils Of copy Method uses the open excel Create a copy of the document

All writes are done in the copy , Until the execution of save(), Write the updated contents in the copy to the original table  

wbook=xlutils.copy(workbook)

4、 Select what you want to write sheet page ,get_sheet The number in the method is sheet The index of the page ( from 0 Start )

In this table ,sheet1 The index of is 0 ,sheet2 The index of is 1.....................

wsheet=wbook.get_sheet(0)

5、 Set the style of the table ( Including line height , Column width , Font style 、 Color, etc )

# Initialize style
style=xlwt.XFStyle()
# Create fonts for styles
font=xlwt.Font()
font.name='new'
font.height=20*10 # font size ,16 For the font size ,20 Is the unit of measurement
style.font=font
# Write column width
wsheet.col(7).width = 3333

6、 write in excel, Parameters of the corresponding That's ok , Column , value , Table style

wsheet.write(2,7,' Ha ha ha ',style)

7、 Update the modified copy to the original form

wbook.save('user.xls')


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved