xlwings模塊怎么合并單元格?
利用python實(shí)現(xiàn)Excel合并單元格
操作代碼如下:# encoding=gbk
import xlsxwriter
# 自定義單元格格式
def set_merge_style(workbook, tag):
number_format, font_color, align = '', '', 'center'
if tag == 'head1':
bold, font_size, border = True, 14, 1
fg_color = '#4F81BD'
font_color = 'white'
else:
bold, font_size, border = True, 12, 1
fg_color = '#FFFFFF'
font_color = 'red'
number_format = '0.00%'
cell_format = workbook.add_format({
'bold': bold,
'font_size': font_size,
'border': border,
'align': align,
'valign': 'vcenter', # 垂直居中
'fg_color': fg_color, # 顏色填充
'num_format': number_format,
'font_color': font_color,
'font_name': '宋體',
})
return cell_format
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet('表格1')
# 合并A列-N列,第一個參數(shù)是位置,第二個參數(shù)是內(nèi)容,第三個參數(shù)是自定義格式
worksheet.merge_range('A1:N2', 'xx模型xx清單',set_merge_style(workbook, tag='head1'))
worksheet.merge_range('A3:N3', '次級標(biāo)題1',set_merge_style(workbook, tag='head2'))
#設(shè)置行高,set_tow(行號,對應(yīng)行高)