【2】文件读写-2-2-python写Excel--xlsxwriter(丰富的格式)

一、参考例子

1.1 xlsxwriter–同一个cell不同格式

#######################################################################
#
# An example of using Python and XlsxWriter to write some "rich strings",
# i.e., strings with multiple formats.
#
# Copyright 2013-2018, John McNamara, jmcnamara@cpan.org
#
import xlsxwriter

workbook = xlsxwriter.Workbook('rich_strings.xlsx')
worksheet = workbook.add_worksheet()

worksheet.set_column('A:A', 30)

# Set up some formats to use.
bold = workbook.add_format({'bold': True})
italic = workbook.add_format({'italic': True})
red = workbook.add_format({'color': 'red'})
blue = workbook.add_format({'color': 'blue'})
center = workbook.add_format({'align': 'center'})
superscript = workbook.add_format({'font_script': 1})

# Write some strings with multiple formats.
worksheet.write_rich_string('A1',
                            'This is ',
                            bold, 'bold',
                            ' and this is ',
                            italic, 'italic')

worksheet.write_rich_string('A3',
                            'This is ',
                            red, 'red',
                            ' and this is ',
                            blue, 'blue')

worksheet.write_rich_string('A5',
                            'Some ',
                            bold, 'bold text',
                            ' centered',
                            center)

worksheet.write_rich_string('A7',
                            italic,
                            'j = k',
                            superscript, '(n-1)',
                            center)

workbook.close()

如果通过List记录参数,可以这样来传递进去

one_list = ['A1','This is ', bold, 'bold',' and this is ',italic, 'italic']
worksheet.write_rich_string(*one_list)

1.2 合并单元格

import xlsxwriter


# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('merge1.xlsx')
worksheet = workbook.add_worksheet()

# Increase the cell size of the merged cells to highlight the formatting.
worksheet.set_column('B:D', 12)
worksheet.set_row(3, 30)
worksheet.set_row(6, 30)
worksheet.set_row(7, 30)


# Create a format to use in the merged range.
merge_format = workbook.add_format({
    'bold': 1,
    'border': 1,
    'align': 'center',
    'valign': 'vcenter',
    'fg_color': 'yellow'})


# Merge 3 cells.
worksheet.merge_range('B4:D4', 'Merged Range', merge_format)

# Merge 3 cells over two rows.
worksheet.merge_range('B7:D8', 'Merged Range', merge_format)


workbook.close()

二、我的案例

案例1: 表头标黄

workbook = xlsxwriter.Workbook(result_fp)
    worksheet = workbook.add_worksheet(sheetname)
    header_format = workbook.add_format({
        'bold': 1,

        'border': 1,
        'align': 'center',
        'valign': 'vcenter',
        'fg_color': 'yellow',
        'font_size': 12,
    })

    column_list = list(df_total.columns.values)
    worksheet.set_column(0, 1 + len(column_list), 30)


    header_format.set_align('hjustify')

    cell_format = workbook.add_format({
        'align': 'center',
        'valign': 'vcenter'
    })

    for jj in range(len(column_list)):
        worksheet.write(0, jj, column_list[jj], header_format)
        df_total[column_list[jj]] = df_total[column_list[jj]].astype(str)

    for index_1, row_1 in df_total.iterrows():
        if index_1 == 0:
            continue
        for jj in range(len(column_list)):
            one_value = row_1[column_list[jj]]
            # if np.isnan(one_value):
            if one_value == 'na':
                one_value = ''
            else:
                if jj == 1 or jj == 2:
                    one_value = float('%.2f' % float(one_value))
                elif jj == 3 or jj == 4:
                    one_value = int(one_value)
            # else:
            #     one_value = row_1[column_list[jj]]
            worksheet.write(index_1, jj, one_value, cell_format)
    workbook.close()

参考资料

这里是一个广告位,,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn