Hi All,


I am using the following code to write data into an excel sheet:

# Open the Output Spreadsheet
objExcel = win32com.client.Dispatch("Excel.Application")

# Creating object to write to Spreadsheet    
        self.xlApp =Dispatch("Excel.Application")
                    
        # Creating Workbook
        self.Wkbk = self.xlApp.Workbooks.Add()

        # Get sheet count
        intShtCnt=self.Wkbk.Worksheets.Count

        # Creating worksheets
        wsObjReport = self.Wkbk.Worksheets.Add()

        # Setting names to the sheets
        wsObjReport.Name="Report"
                   
        # Alignment Variables
        xlLeft, xlRight, xlCenter = -4131, -4152, -4108

        # Loop to remove extra sheets(created by default)
        for intiLoopIndex in range(1,intShtCnt+1):
            self.Wkbk.Worksheets("Sheet"+str(intiLoopIndex)).Delete()        

        # Setting active sheet
        sheets = self.Wkbk.Sheets
        sheets("Report").Activate()

I am able to change the default sheet name to "Report". I want to change the color of the excel sheet from WHITE to YELLOW. How can I do it?...
Any help is much appreciated.

Regards,
Dinil

Dani AI

Generated

you can’t change a worksheet’s background color itself. Excel lets you color cell interiors, color the sheet tab, or set a background picture. So ’s approach is the right direction. If your goal is a yellow canvas behind your data, fill the cells you actually use (or will use), or color the tab if you just want a visual cue. See Interior.Color and ColorIndex, Tab.Color, and UsedRange in the VBA docs for details. Interior.Color, Tab.Color, Worksheet.UsedRange

Here is a safe pattern you can drop in after you finish writing the data to "Report". It colors only the used cells (faster than touching all 1M rows), and also shows how to color the sheet tab:

from win32com.client import constants

ws = self.Wkbk.Worksheets("Report")

# Speed up while formatting
xl = ws.Parent.Application
prev_calc = xl.Calculation
try:
    xl.ScreenUpdating = False
    xl.Calculation = constants.xlCalculationManual

    # Yellow fill for used cells
    ws.UsedRange.Interior.Color = 65535  # RGB(255,255,0)

    # Optional: yellow sheet tab
    ws.Tab.Color = 65535  # or use .ColorIndex = 6
finally:
    xl.Calculation = prev_calc
    xl.ScreenUpdating = True

A few tips:

  • Avoid ws.Cells.Interior... on the whole sheet; it is slow and can bloat the file. Target UsedRange or the specific range you populate. UsedRange
  • To clear the fill later: ws.UsedRange.Interior.Pattern = constants.xlPatternNone. Interior.Color
  • If you wanted a watermark-style background instead of a color, Excel only supports a background picture: ws.SetBackgroundPicture(path). Worksheet.SetBackgroundPicture
  • Turning off screen updates and switching to manual calc around bulk formatting can make COM automation much faster. Application.ScreenUpdating, Application.Calculation

Recommended Answers

All 2 Replies

I am not sure whether you can change the entire sheet color to yellow.
But you can change the colors of individual cells to any color u want.

worksheet.Cells(row,column).interior.colorindex = 6

here 6 is for yellow.

http://www.geocities.com/davemcritchie/excel/colors.htm

you can use the above URL for other color indexes.

Thanks a lot brother.. Its working fine.. I am now able to change the color of each row.
Thanks once again!...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.