M
M
Magic Code2020-08-17 14:20:11
Python
Magic Code, 2020-08-17 14:20:11

How to color a cell in an excel table when writing data?

Good afternoon!
Can you please tell me how can I color a certain cell when writing?

def to_xlsx(vacancy_list: list) -> 'excel':
    workbook = xlsxwriter.Workbook('vacancy.xlsx')
    worksheet = workbook.add_worksheet()
    # задаем цвет фона ячейки
    cell_format = workbook.add_format()
    cell_format.set_bg_color('gray')
    cell = 1
    cell_format_num = 1
    for job_cards in vacancy_list:
        for title in job_cards:
            worksheet.write(f'A{cell}', title, cell_format, )  # будет закрашивать каждую ячейку
            # worksheet.write(f'A{cell_format_num}', title, cell_format,) - закрасит 1 ячейку
            worksheet.write(f'B{cell}', job_cards[title],)
            cell += 1
            # надо закрасить каждую седьмую ячейку, начиная с 1
            cell_format_num += 6
    workbook.close()

An example of how it should turn out:5f3a67ab88936580469217.jpeg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Magic Code, 2020-08-17
@Panda_Code

The issue is resolved as follows:

cell = 1
with xlsxwriter.Workbook('vacancy.xlsx') as workbook:
    worksheet = workbook.add_worksheet()
    data_format = workbook.add_format({'bg_color': '#FFC7CE'})
    for item in range(len(jobs_cards)):
        for key, value in (jobs_cards[item].items()):
            worksheet.write(f'A{cell}', key)
            worksheet.write(f'B{cell}', value)
            cell += 1
        for row in range(0, 13, 6):
            worksheet.set_row(row, cell_format=data_format)

Z
zexer, 2020-08-17
@zexer

https://stackoverflow.com/questions/10532367/how-t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question