outputting a coloured excel file python

0 votes
I've been asked to create a program for the business I work for that will take an excel file, change any filled-out rows to have a pink background, and then save the file. I'm really having trouble with this, so if anyone has any advice or basic functions I can base my code on, that would be greatly appreciated.
Feb 26, 2023 in Others by narikkadan
• 63,420 points
163 views

1 answer to this question.

0 votes

If you already have an Excel workbook, you can read it in and make changes right there. However, until you get the hang of this, it might be preferable to maintain the original and make a new one that you can discard without losing data.

It is a multi-step process:

Create the workbook and save it into a variable:

workbook = xlsxwriter.Workbook('sample.xlsx')

Add a worksheet to the workbook

worksheet = workbook.add_worksheet()

This worksheet now lives as a sheet in the actual Excel workbook as well as an object in Python's memory that is referenced by the worksheet.

You must now enter some information on that blank page:

worksheet.write(pandas_df) # this is where you drop in the data

Set a format to apply to your selection:

formats = workbook.add_format({'bg_color': '#F9DCD6',
                               'font_color': '#095A03'})

The next part is what you need to experiment with:

worksheet.conditional_format('A1:Z100', {'type': 'cell',
                                     'criteria': '<your criteria>',
                                     'value': <your value>,
                                     'format': formats})

You can look up all the ways to identify different sorts of data here: xlsxwriter

Within this, there should be some way to create the scheme you are seeking.

answered Mar 17, 2023 by Kithuzzz
• 38,010 points

Related Questions In Others

0 votes
1 answer

Export a simple Dictionary into Excel file in python

You can use pandas. import pandas as pd dict1 ...READ MORE

answered Oct 23, 2022 in Others by narikkadan
• 63,420 points
5,346 views
0 votes
1 answer

Add a correct format date in Excel file with Python openpyxl

Use just datetime.fromisoformat(row['Date']) but I didn't have ...READ MORE

answered Feb 9, 2023 in Others by narikkadan
• 63,420 points
2,072 views
0 votes
1 answer

Openpyxl password protect excel file python

Please refer to the documentation here. Workbooks can be ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,420 points
6,704 views
0 votes
1 answer

Is there any way in python to auto-correct spelling mistake in multiple rows of an excel files of a single column?

Use Spellchecker for doing your stuff: import pandas ...READ MORE

answered Oct 14, 2022 in Others by narikkadan
• 63,420 points
1,583 views
0 votes
1 answer

Python: Issue with 'unexpected end of pattern'

I should start by stating that using ...READ MORE

answered Sep 12, 2018 in Python by Priyaj
• 58,090 points
2,325 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,051 views
0 votes
1 answer

How to read an Excel CSV file in Python?

The csv module or the pandas library ...READ MORE

answered Mar 19, 2023 in Others by Kithuzzz
• 38,010 points
505 views
0 votes
1 answer

Copying a sheet with specific formatting from an excel file to a new output

Using Python and the openpyxl package, you ...READ MORE

answered Mar 31, 2023 in Others by Kithuzzz
• 38,010 points
7,554 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP