excel for xlwt operation in python

Keywords: Python encoding Excel Java

In python, using xlwt to operate excel is very square. Compared with Java using apache poi, this is paradise
I. install xlwt module

pip3 install xlwt

2. Simple use of xlwt

import xlwt  #Import module
workbook = xlwt.Workbook(encoding='utf-8') #Create a workbook object
worksheet = workbook.add_sheet('sheet1')   #Create sheet sheet sheet
worksheet.write(0, 0, 'hello') #Write content to the table, the first parameter row, the second parameter column, and the third parameter content
workbook.save('students.xls') #Save table as students.xls
#When using poi operation excel in Java to create the workbook object, you need to write the table name. After creating the workbook object
#This table already exists,But use xlwt You need to name the table when the last call is saved.,And only transfer in save Only after method#Create table

3. Set style for content

workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('sheet1')
#Set font style
font = xlwt.Font()
#Typeface
font.name = 'Time New Roman'
#Thickening
font.bold = True
#Underline
font.underline = True
#Italics
font.italic = True

#Create style
style = xlwt.XFStyle()
style.font = font
#Create a workbook based on style
worksheet.write(0, 1, 'world', style)
workbook.save('students.xls')

4. Merge cells. When using xlwt to merge cells, you can create and merge cells directly without using the style setting of poi,

workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('sheet1')
#Create merged cells by calling merge() from the worksheet
#The first and second parameter single table rows are merged, and the third and fourth parameter columns are merged,

#Merge cells from column 0 to 2
worksheet.write_merge(0, 0, 0, 2, 'first merge')

#Merge cells in row 1, row 2, column 1
worksheet.write_merge(0, 1, 0, 0, 'first merge')

workbook.save('students.xls')

If you need to know the specific rules of cell merging, try to merge by yourself to see the merging effect, so as to be clear

V. set cell alignment

workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('sheet1')
alignment = xlwt.Alignment()
# horizontally
alignment.horz = xlwt.Alignment.HORZ_CENTER
# Vertical centering
alignment.vert = xlwt.Alignment.VERT_CENTER
style = xlwt.XFStyle()
style.alignment = alignment
#Set cell width
worksheet.col(0).width = 6666

#Set cell height
worksheet.row(0).height_mismatch = True
worksheet.row(0).height = 1000

worksheet.write(0, 0, 'hello world', style)
workbook.save('center.xls')

Vi. set the unit style frame

workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('sheet1')
border = xlwt.Borders()
# DASHED dotted line
# No line
# THIN solid line
border.left = xlwt.Borders.THIN
# Set color
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')

7. Set cell border

workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('sheet1')

border = xlwt.Borders()
# DASHED dotted line
# No line
# THIN solid line
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')

8. Set cell background color

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')

IX. set font color

workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('sheet1')

font = xlwt.Font()
# Set font to red
font.colour_index=xlwt.Style.colour_map['red']

style = xlwt.XFStyle()

style.font = font

worksheet.write(0, 1, 'world', style)
workbook.save('students.xls')

Problems encountered:

1.PermissionError: [Errno 13] Permission denied: 'dashed.xls'
Reason: the file is opened and cannot be written during the test write call. It is ok to write after the file is closed
2.TypeError: 'module' object is not callable
 When creating a style, the error object will
style = xlwt.Style()

Change to

style = xlwt.XFStyle()

Posted by Thatsmej on Sat, 30 Nov 2019 15:39:38 -0800