workbook = xlwt.Workbook(encoding='ascii')
worksheet = workbook.add_sheet('sheet', cell_overwrite_ok=True) # 可以创建多个工作表以及设置任意的工作表名
初始化样式
style = xlwt.XFStyle()
# 建立单元格数据类型,数值类型,默认为:'General'
style.num_format_str = '0'
数值类型索引如下
# 单元格数据类型 #######################################################
'''
'general',
'0',
'0.00',
'#,##0',
'#,##0.00',
'"$"#,##0_);("$"#,##0)',
'"$"#,##0_);[Red]("$"#,##0)',
'"$"#,##0.00_);("$"#,##0.00)',
'"$"#,##0.00_);[Red]("$"#,##0.00)',
'0%',
'0.00%',
'0.00E+00',
'# ?/?',
'# ??/??',
'M/D/YY',
'D-MMM-YY',
'D-MMM',
'MMM-YY',
'h:mm AM/PM',
'h:mm:ss AM/PM',
'h:mm',
'h:mm:ss',
'M/D/YY h:mm',
'_(#,##0_);(#,##0)',
'_(#,##0_);[Red](#,##0)',
'_(#,##0.00_);(#,##0.00)',
'_(#,##0.00_);[Red](#,##0.00)',
'_("$"* #,##0_);_("$"* (#,##0);_("$"* "-"_);_(@_)',
'_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)',
'_("$"* #,##0.00_);_("$"* (#,##0.00);_("$"* "-"??_);_(@_)',
'_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)',
'mm:ss',
'[h]:mm:ss',
'mm:ss.0',
'##0.0E+0',
'@'
'''
默认设置
# self.height = 0x00C8 # 200: this is font with height 10 points
# self.italic = False
# self.struck_out = False
# self.outline = False
# self.shadow = False
# self.colour_index = 0x7FFF #
# self.bold = False
# self._weight = 0x0190 # 0x02BC gives bold font
# self.escapement = self.ESCAPEMENT_NONE
# self.underline = self.UNDERLINE_NONE
# self.family = self.FAMILY_NONE
# self.charset = self.CHARSET_SYS_DEFAULT
# self.name = 'Arial'
自定义设置
font = xlwt.Font() # 为样式创建字体
# 字体类型:比如宋体、仿宋也可以是汉仪瘦金书繁
font.name = name
# 字体大小
font.height = 20 * n # 如10号字体,n=10
# 设置字体颜色、蓝色
font.colour_index = 0x0C
# 设置加粗
font.bold = True
# 字体中是否有横线struck_out
font.struck_out =True
# 设置斜体
font.italic = True
# 设置删除线
font.struck_out = True
# 设置轮廓
font.outline = True
# 设置阴影
font.shadow = True
# 设置字体上下标、上标
# font.escapement = 1
# 设置下划线、双层下划线,当值为11时。填充颜色就是蓝色
font.underline = 2
# 设置字符集、GBK
font.charset = 0x86
# 定义字体格式
style.font = font
字体颜色设置如下选择
# 颜色索引 #######################################################
"""
aqua 0x31
black 0x08
blue 0x0C
blue_gray 0x36
bright_green 0x0B
brown 0x3C
coral 0x1D
cyan_ega 0x0F
dark_blue 0x12
dark_blue_ega 0x12
dark_green 0x3A
dark_green_ega 0x11
dark_purple 0x1C
dark_red 0x10
dark_red_ega 0x10
dark_teal 0x38
dark_yellow 0x13
gold 0x33
gray_ega 0x17
gray25 0x16
gray40 0x37
gray50 0x17
gray80 0x3F
green 0x11
ice_blue 0x1F
indigo 0x3E
ivory 0x1A
lavender 0x2E
light_blue 0x30
light_green 0x2A
light_orange 0x34
light_turquoise 0x29
light_yellow 0x2B
lime 0x32
magenta_ega 0x0E
ocean_blue 0x1E
olive_ega 0x13
olive_green 0x3B
orange 0x35
pale_blue 0x2C
periwinkle 0x18
pink 0x0E
plum 0x3D
purple_ega 0x14
red 0x0A
rose 0x2D
sea_green 0x39
silver_ega 0x16
sky_blue 0x28
tan 0x2F
teal 0x15
teal_ega 0x15
turquoise 0x0F
violet 0x14
white 0x09
yellow 0x0D
"""
说明
这里的参数变量使用了十六进制,可以将其输入十进制,比如想设置字体的颜色为black,可以使用 0x08
也可以 8
下同:有些可能使用了八进制,可以查看函数说明。(十六进制以'0x'
开头;八进制以'0'
开头)
上下标、下划线、字符集索引:
ESCAPEMENT_NONE = 0x00
# 上标
ESCAPEMENT_SUPERSCRIPT = 0x01
# 下标
ESCAPEMENT_SUBSCRIPT = 0x02
# 下划线可选值
UNDERLINE_NONE = 0x00
UNDERLINE_SINGLE = 0x01
UNDERLINE_SINGLE_ACC = 0x21
UNDERLINE_DOUBLE = 0x02
UNDERLINE_DOUBLE_ACC = 0x22
# 字符集可选值
CHARSET_ANSI_LATIN = 0x00
CHARSET_SYS_DEFAULT = 0x01
CHARSET_SYMBOL = 0x02
CHARSET_APPLE_ROMAN = 0x4D
CHARSET_ANSI_JAP_SHIFT_JIS = 0x80
CHARSET_ANSI_KOR_HANGUL = 0x81
CHARSET_ANSI_KOR_JOHAB = 0x82
CHARSET_ANSI_CHINESE_GBK = 0x86
CHARSET_ANSI_CHINESE_BIG5 = 0x88
CHARSET_ANSI_GREEK = 0xA1
CHARSET_ANSI_TURKISH = 0xA2
CHARSET_ANSI_VIETNAMESE = 0xA3
CHARSET_ANSI_HEBREW = 0xB1
CHARSET_ANSI_ARABIC = 0xB2
CHARSET_ANSI_BALTIC = 0xBA
CHARSET_ANSI_CYRILLIC = 0xCC
CHARSET_ANSI_THAI = 0xDE
CHARSET_ANSI_LATIN_II = 0xEE
CHARSET_OEM_LATIN_I = 0xFF
除了可以设置单元格格式外,还可以通过_search_key()
方法来获取单元格样式信息。
如:(以下其他样式信息的获取方法相同)
font_info = font._search_key()
默认设置:
# self.left = self.NO_LINE
# self.right = self.NO_LINE
# self.top = self.NO_LINE
# self.bottom = self.NO_LINE
# self.diag = self.NO_LINE
# self.left_colour = 0x40
# self.right_colour = 0x40
# self.top_colour = 0x40
# self.bottom_colour = 0x40
# self.diag_colour = 0x40
# self.need_diag1 = self.NO_NEED_DIAG1
# self.need_diag2 = self.NO_NEED_DIAG2
自定义设置
borders = xlwt.Borders()
borders.left =NO_LINE
borders.left = xlwt.Borders.THIN
# NO_LINE: 官方代码中NO_LINE所表示的值为0,没有边框
# THIN: 官方代码中THIN所表示的值为1,边框为实线
# 左边框 细线
borders.left = 1
# 右边框 中细线
borders.right = 2
# 上边框 虚线
borders.top = 3
# 下边框 点线
borders.bottom = 4
# 内边框 粗线
borders.diag = 5
# 左边框颜色 蓝色
borders.left_colour = 0x0C
# 右边框颜色 金色
borders.right_colour = 0x33
# 上边框颜色 绿色
borders.top_colour = 0x11
# 下边框颜色 红色
borders.bottom_colour = 0x0A
# 内边框 黄色
borders.diag_colour = 0x0D
# 定义格式
style.borders = borders
边框线型索引:
NO_LINE = 0x00
THIN = 0x01
MEDIUM = 0x02
DASHED = 0x03
DOTTED = 0x04
THICK = 0x05
DOUBLE = 0x06
HAIR = 0x07
#The following for BIFF8
MEDIUM_DASHED = 0x08
THIN_DASH_DOTTED = 0x09
MEDIUM_DASH_DOTTED = 0x0A
THIN_DASH_DOT_DOTTED = 0x0B
MEDIUM_DASH_DOT_DOTTED = 0x0C
SLANTED_MEDIUM_DASH_DOTTED = 0x0D
默认设置为:
# self.pattern = self.NO_PATTERN # 无填充
# self.pattern_fore_colour = 0x40
# self.pattern_back_colour = 0x41
自定义设置:
pattern = xlwt.Pattern()
# 设置背景颜色的模式(打开填充)
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pat.pattern = 1
# 两种模式
# NO_PATTERN = 0x00
# SOLID_PATTERN = 0x01
# 填充前景色、紫色
pat.pattern_fore_colour = 0x14 # 看上述的颜色索引
# 填充背景色、紫色
pat.pattern_back_colour = 0x14
style.pattern = pattern
默认设置
# self.horz = self.HORZ_GENERAL
# self.vert = self.VERT_BOTTOM
# self.dire = self.DIRECTION_GENERAL
# self.orie = self.ORIENTATION_NOT_ROTATED
# self.rota = self.ROTATION_0_ANGLE
# self.wrap = self.NOT_WRAP_AT_RIGHT
# self.shri = self.NOT_SHRINK_TO_FIT
# self.inde = 0
# self.merg = 0
自定义设置
alignment = xlwt.Alignment() # 居中
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
# 旋转方向,设置旋转方向45
alignment.rota = 45
# 自动缩进设置
alignment.shri = 1
style.alignment = alignment # 设定样式
其他对齐方式索引:
# 水平对齐方式
HORZ_GENERAL = 0x00
HORZ_LEFT = 0x01
HORZ_CENTER = 0x02
HORZ_RIGHT = 0x03
HORZ_FILLED = 0x04
HORZ_JUSTIFIED = 0x05 # BIFF4-BIFF8X
HORZ_CENTER_ACROSS_SEL = 0x06 # Centred across selection (BIFF4-BIFF8X)
HORZ_DISTRIBUTED = 0x07 # Distributed (BIFF8X)
# 垂直对齐方式
VERT_TOP = 0x00
VERT_CENTER = 0x01
VERT_BOTTOM = 0x02
VERT_JUSTIFIED = 0x03 # Justified (BIFF5-BIFF8X)
VERT_DISTRIBUTED = 0x04 # Distributed (BIFF8X)
# 旋转角度
ROTATION_0_ANGLE = 0x00
ROTATION_STACKED = 0xFF
# 自动缩进设置
SHRINK_TO_FIT = 0x01
NOT_SHRINK_TO_FIT = 0x00
默认设置:
# self.cell_locked = 1
# self.formula_hidden = 0
自定义设置:
# 设置单元格锁定
protection.cell_locked = 1
# 设定隐藏单元格内公式
protection.formula_hidden = 1
# 只有在sheet表设置为保护时才有效
worksheet.set_protect(1)
参数:row
:行; col
:列; element
:写入元素; style
:样式设置
worksheet.write(row, col, element, style) # 参数:row:行; col:列; element:写入元素; style :样式设置
参数:row_start
:开始行;row_end
:结束行; col_start
:开始列; col_end
:结束列; element
:写入元素; style
:样式设置
worksheet.write_merge(row_start, row_end, col_start, col_end, element, style)
for i in range(ncol): # ncol:列数
worksheet.col(i).width = 256 * width # 设置宽度
也可以worksheet.col(i).width
对指定某列设置
workbook.save(r'D:\demo.xls')
扩展
:通过函数来设定不同的样式# 样式设置
def set_Style(name,size,color,borders_size,color_fore,blod=False):
style = xlwt.XFStyle() # 初始化样式
# 字体
font = xlwt.Font()
font.name = name
font.height = 20 * size # 字号
font.bold = blod # 加粗
font.colour_index = color # 默认:0x7FFF 黑色:0x08
style.font = font
# 居中
alignment = xlwt.Alignment() # 居中
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
style.alignment=alignment
# 边框
borders = xlwt.Borders()
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
borders.top = xlwt.Borders.THIN
borders.bottom = borders_size # 自定义:1:细线;2:中细线;3:虚线;4:点线
style.borders = borders
# 背景颜色
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN # 设置背景颜色的模式(NO_PATTERN; SOLID_PATTERN)
pattern.pattern_fore_colour = color_fore # 默认:无色:0x7FFF;黄色:0x0D;蓝色:0x0C
style.pattern = pattern
return style
book_title1 = ['沥青路面病害明细表(标准)']
book_title2 = ['路线编码','路幅','车道','路面类型','图片桩号','图片序号', '病害标准判别号', '病害标准判别']
# 合并单元格写入表头
worksheet_ori.write_merge(0, 0, 0, 8, book_title1, set_Style('宋体',14,0x7FFF,0,0x7FFF,blod=True))
# 写入指定的副表头
for _num, _title in enumerate(book_title2):
worksheet_ori.write(1, _num, _title, set_Style('宋体',12,0x08,2,0x7FFF,blod=True))
python的xlwt
库的使用暂时写到这,后面会继续完善其他的用法。
以下链接的博主写得也非常详细!
参考链接:https://blog.csdn.net/zhouz92/article/details/106833511