我想创建一个通用格式函数,top,bottom,right,left的默认值是什么

问题描述 投票:-1回答:2
def columnandcellformate(sheet_name,bold = 0,font_color = '#000000',bg_color = '#ffffff',align = '' ,bottom = 0 ,top = 3,right = 0,left = 0,font_size = 10 ,starcolumn = 0, endrow = 0 ):
    global sheet_format
    sheet_format=sheet_name.add_format({
                        'bottom':bottom,
                        'top' : top,
                        'bg_color':bg_color,
                        'font_color' : font_color,
                        'align':align,
                        'font_size':font_size,
                        'bold': bold,
                        'font_name':'Batang'
                       })

什么是top,bottom,right,left的默认值,我的功能是将单元格顶部,底部,右侧和左侧空白

python xlsx xlsxwriter
2个回答
1
投票

我认为您的默认背景颜色可能导致单元格边框出现问题。我已根据您是否希望通过您的功能调用这些条件添加了一些条件。这些条件使用格式方法,如format.set_bg_color()format.set_bottom()see docs for more information on these)。如果从默认值更改背景颜色,它们仅提供背景颜色。

import xlsxwriter

def columnandcellformate(bold = 0, font_color = '#000000', bg_color = 'none', align = '' , bottom = 999, top = 999, right = 999, left = 999, font_size = 10):
    global sheet_format
    sheet_format=workbook.add_format({
    'font_color' : font_color,
    'align': align,
    'font_size': font_size,
    'bold': bold,
    'font_name': 'Batang'
    })
    if bg_color != 'none':
        sheet_format.set_bg_color(bg_color)
    if bottom != 999:
        sheet_format.set_bottom(bottom)
    if top != 999:
        sheet_format.set_top(top)
    if right != 999:
        sheet_format.set_right(right)
    if left != 999:
        sheet_format.set_left(left)


workbook = xlsxwriter.Workbook('test.xlsx')
ws = workbook.add_worksheet('test_1')

columnandcellformate()
ws.write('B1', 'foo', sheet_format)
columnandcellformate(bold = 1, font_color = '#9C0006', bg_color = '#FFC7CE', align = '', bottom = 2, top = 1, right = 1, left = 1, font_size = 10)
ws.write('B3', 'bar', sheet_format)
workbook.close()

预期产出:

Expected Output of test.xlsx


1
投票

格式属性的默认值几乎都是0 / False。有关格式对象,请参阅initialization code

© www.soinside.com 2019 - 2024. All rights reserved.