Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6944659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:20:07+00:00 2026-05-27T13:20:07+00:00

I want to write to an Excel sheet via pywin32. I can do it

  • 0

I want to write to an Excel sheet via pywin32. I can do it actually without problem. But I couldnt format a range of cells in sheet. I want to align the values centerly inside cells. And also i need to fill the cells with color. How can I do it?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:20:08+00:00Added an answer on May 27, 2026 at 1:20 pm

    I’ve not specifically done this using python before, but I’m assuming you’re using the COM automation interface to excel.

    This page has an example that seems to cover both alignment and filling cells with colour in C#, so it should be fairly easy to adapt to python. Assuming you have a Worksheet object called sheet, and the Excel automation object is called Excel, I’m guessing it might look a bit like this:

    //Format A1:D1 as center alignment, 
    sheet.Range("A1", "D1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
    sheet.Range("A1", "D1").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter
    sheet.Range("A1", "D1").Interior.ColorIndex = Excel.XlColorIndex.Red
    

    If you don’t have access to the Excel.XlAlign and XlColorIndex constants from python then you can just replace them with the specific integers they represent, though I’m not entirey sure where you could get them from. Probably from a VBA Reference Site or similar. (Though that link I provided doesn’t seem to allow you to expand each of the entries in the list, so you may need to look elsewhere)

    EDIT: Just had a play about with excel automation via the python console, and it seems to work alright:

    >>> from win32com.client import Dispatch
    >>> xlApp = Dispatch("Excel.Application")
    >>> xlWb = xlApp.Workbooks.Add()
    >>> xlSht = xlWb.WorkSheets(1)
    >>> xlSht.Range("A1", "D1").VerticalAlignment = 1
    >>> xlSht.Range("A1", "D1").Interior.ColorIndex = 6
    >>> # The background color of A1-D1 should now be yellow
    >>> xlSht.Cells(1, 1).VerticalAlignment = 1
    

    If you can’t find any good reference on what the various alignment/colour constants are, then I’d just play about with python on the console like this, then open the resulting worksheet in excel and have a look at the results to figure things out.

    You can find the official reference for the office 2003 automation API here
    Specifically, you’ll probably find the range documentation most usefull.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use Microsoft.Office.Interop.Excel dll to write the data into excel sheet. I
I have an Excel sheet with cells containing html. How can I batch convert
I have an excel invoice sheet and I want to write the information from
I'm using Java and OpenXLS to write out an Excel spreadsheet. I want to
I want to merge two cells in excel using pyExcelerator , ws.write_merge(r1=0,r2=1,c1=0, c2=0, label='test1',
With Spreadsheet::WriteExcel , I can create a new workbook, but what if I want
I want to write a Swing application in Griffon but I am not sure
I want to write a little DBQuery function in perl so I can have
I am writing an Excel File using Apache POI. I want to write in
I need to write the data from an Excel sheet/workbook to the text file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.