synopsis
Write to Excle document
Installation: pip3 install xlwt
Import: import xlwt
xlrd Module Method
Write Case
import xlwt
# Create objects, set encoding
workbook = (encoding='utf-8')# Create a new sheet
worksheet = workbook.add_sheet(sheet_name, cell_overwrite_ok=True)# Write values in the corresponding rows and columns
(0,0, label = 'this is test')
# Save
('Excel_test.xls')
Setting font properties
font = () # create a font object to hold the operations performed on the font
= 'Microsoft YaHei' # Font set to 'Microsoft YaHei'
= True # Bold font
= True # Fonts are underlined
= True # Font Skew
style = () # create a style object to hold the style of the excel
= font # Save font information to the style object
(0, 0, 'no font format')
# Add content 'with font formatting' with font attribute in cell with coordinates 1,0
(1, 0, 'with font formatting', style)
Setting the background
# Create a pattern object to hold the cell background style
pattern = ()
# Setting the background pattern style of cells (18 styles from 0x01-0x12)
= 0x01
# Set the background color of the cell
pattern.pattern_fore_colour = .colour_map['yellow']
style = ()
= pattern # Save background color information to the styke object
# Add content 'with background' in cell with coordinates 0,0 with background color
(0, 0, 'has background color', style)
Merge Cells
# Merge two columns with coordinates 0,0 vertical and 0,1 horizontal and add content 'Merge two columns'
worksheet.write_merge(0, 0, 0, 1, 'Merge two columns')# Merge two lines with vertical coordinates 1,0 and horizontal coordinates 2,0 and add content 'Merge two lines'
worksheet.write_merge(1, 2, 0, 0, 'Merge two rows')# Merge three rows and three columns with vertical coordinates 3-5 and horizontal coordinates 0-2 and add content 'Merge three rows and three columns'
worksheet.write_merge(3, 5, 0, 2, 'Merge three rows and three columns')
Other methods
# Set cell width
(0).width = 200
This is the whole content of this article.