python中使用xlwt操做excel很是方,和Java使用調框架apache poi相比這就是天堂啊
一.安裝xlwt模塊python
pip3 install xlwt
二.簡單使用xlwtapache
import xlwt #導入模塊 workbook = xlwt.Workbook(encoding='utf-8') #建立workbook 對象 worksheet = workbook.add_sheet('sheet1') #建立工做表sheet worksheet.write(0, 0, 'hello') #往表中寫內容,第一各參數 行,第二個參數列,第三個參數內容 workbook.save('students.xls') #保存表爲students.xls #在Java中使用poi操做excel 在建立workbook對象的時候就須要寫好表名,建立workbook對象完成後 #這個表就已經存在,可是使用xlwt 須要在最後調用保存的時候爲表命名,而且只有調入save方法後纔會#建立表
三.爲內容設置style框架
workbook = xlwt.Workbook(encoding='utf-8') worksheet = workbook.add_sheet('sheet1') #設置字體樣式 font = xlwt.Font() #字體 font.name = 'Time New Roman' #加粗 font.bold = True #下劃線 font.underline = True #斜體 font.italic = True #建立style style = xlwt.XFStyle() style.font = font #根據樣式建立workbook worksheet.write(0, 1, 'world', style) workbook.save('students.xls')
四.合併單元格,使用xlwt 合併單元格時不用像poi經過style來設置,直接建立和並單元格就能夠,測試
workbook = xlwt.Workbook(encoding='utf-8') worksheet = workbook.add_sheet('sheet1') #經過worksheet調用merge()建立合併單元格 #第一個和第二個參數單錶行合併,第三個和第四個參數列合併, #合併第0列到第2列的單元格 worksheet.write_merge(0, 0, 0, 2, 'first merge') #合併第1行第2行第一列的單元格 worksheet.write_merge(0, 1, 0, 0, 'first merge') workbook.save('students.xls')
若是須要了解具體調合並單元格規則就本身試着合併,查看合併效果.才能清晰明瞭字體
五.設置單元格的對齊方式excel
workbook = xlwt.Workbook(encoding='utf-8') worksheet = workbook.add_sheet('sheet1') alignment = xlwt.Alignment() # 水平居中 alignment.horz = xlwt.Alignment.HORZ_CENTER # 垂直居中 alignment.vert = xlwt.Alignment.VERT_CENTER style = xlwt.XFStyle() style.alignment = alignment #設置單元格寬度 worksheet.col(0).width = 6666 #設置單元格的高度 worksheet.row(0).height_mismatch = True worksheet.row(0).height = 1000 worksheet.write(0, 0, 'hello world', style) workbook.save('center.xls')
六.設置單元格調邊框code
workbook = xlwt.Workbook(encoding='utf-8') worksheet = workbook.add_sheet('sheet1') border = xlwt.Borders() # DASHED虛線 # NO_LINE沒有 # THIN實線 border.left = xlwt.Borders.THIN # 設置顏色 border.left_coloure = 0x40b border.right = xlwt.Borders.THIN border.right_colour = 0x40b border.top = xlwt.Borders.THIN border.top_colour = 0x40b border.bottom = xlwt.Borders.THIN border.bottom_colour = 0x40b style = xlwt.XFStyle() style.borders = border worksheet.write(0, 0, 'love', style) workbook.save('dashed.xls')
七.設置單元格邊框對象
workbook = xlwt.Workbook(encoding='utf-8') worksheet = workbook.add_sheet('sheet1') border = xlwt.Borders() # DASHED虛線 # NO_LINE沒有 # THIN實線 border.left = xlwt.Borders.THIN border.right = xlwt.Borders.THIN border.top = xlwt.Borders.THIN border.bottom = xlwt.Borders.THIN style = xlwt.XFStyle() style.borders = border worksheet.write(1, 1, 'love', style) workbook.save('dashed.xls')
八.設置單元格背景色ip
workbook = xlwt.Workbook(encoding='utf-8') worksheet = workbook.add_sheet('sheet1') pattern = xlwt.Pattern() pattern.pattern = xlwt.Pattern.SOLID_PATTERN # 8 through 63 # 0 = Black, 1 = White, # 2 = Red, 3 = Green, 4 = Blue, # 5 = Yellow, 6 = Magenta, 7 = Cyan, # 16 = Maroon, 17 = Dark Green, # 18 = Dark Blue, 19 = Dark Yellow , # almost brown), 20 = Dark Magenta, # 21 = Teal, 22 = Light Gray, # 23 = Dark Gray, the list goes on... pattern.pattern_fore_colour = 3 style = xlwt.XFStyle() style.pattern = pattern worksheet.write(1, 1, 'shit', style) workbook.save('shit.xls')
九.設置字體顏色utf-8
workbook = xlwt.Workbook(encoding='utf-8') worksheet = workbook.add_sheet('sheet1') font = xlwt.Font() # 設置字體爲紅色 font.colour_index=xlwt.Style.colour_map['red'] style = xlwt.XFStyle() style.font = font worksheet.write(0, 1, 'world', style) workbook.save('students.xls')
遇到的問題:
1.PermissionError: [Errno 13] Permission denied: 'dashed.xls' 緣由:測試寫入調時候文件被打開了,沒法寫入,關閉文件後再寫入就ok了 2.TypeError: 'module' object is not callable 建立style的時候調錯對象將
style = xlwt.Style()
改成
style = xlwt.XFStyle()