Answer the question
In order to leave comments, you need to log in
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()
Answer the question
In order to leave comments, you need to log in
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)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question