A
A
albertalexandrov2020-08-27 21:43:36
Python
albertalexandrov, 2020-08-27 21:43:36

Why is xlsxwriter not applying align and text_wrap styles?

Hello. There is a code that creates an excel file:

import xlsxwriter

workbook = xlsxwriter.Workbook('formatting.xlsx')
worksheet1 = workbook.add_worksheet()

format1 = workbook.add_format({'align': 'center', 'bg_color': '#FFC7CE', 'font_color': '#9C0006'})
format2 = workbook.add_format({'align': 'center', 'bg_color': '#C6EFCE', 'font_color': '#006100'})

data = [
    [34, 72, 38, 30, 75, 48, 75, 66, 84, 86],
    [6, 24, 1, 84, 54, 62, 60, 3, 26, 59],
    [28, 79, 97, 13, 85, 93, 93, 22, 5, 14],
]

worksheet1.write('A1', 'Cells with values >= 50 are in light red. Values < 50 are in light green.')

for row, row_data in enumerate(data):
    worksheet1.write_row(row + 2, 1, row_data)

worksheet1.conditional_format('B3:K12', {
    'type': 'cell', 'criteria': '>=',
    'value': 50,
    'format': format1
})

worksheet1.conditional_format('B3:K12', {
    'type': 'cell',
    'criteria': '<',
    'value': 50,
    'format': format2
})


The problem is that this does not apply the style 'align': 'center'(also text_wrap), but the text needs to be centered. At the same time bg_color, they font_colorapply.

The main task is to apply align and text_wrap to a given range of cells.

Please advise what is wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Skorik, 2020-11-23
@Alex_Cat

Same problem. The author did not understand why centering does not work?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question