Search code examples
pythonxlsxxlsxwriter

I want to create a genric format function, what is default value of top,bottom,right,left


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'
                       })

What is default value of top,bottom,right,left, My function is making cell top,bottom,right and left blank


Solution

  • I think your default background color may have been causing some issues with the cell borders. I've added a few conditions based on whether you want these called by your function or not. These conditions make use of Format Methods such as format.set_bg_color(), format.set_bottom() (see docs for more information on these). They only provide a background color if you change it from the default.

    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:

    Expected Output of test.xlsx