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 7403095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:58:07+00:00 2026-05-29T04:58:07+00:00

I am trying to create an Excel workbook where I can auto-set, or auto-adjust

  • 0

I am trying to create an Excel workbook where I can auto-set, or auto-adjust the widths of the columns before saving the workbook.

I have been reading the Python-Excel tutorial in hopes of finding some functions in xlwt that emulate xlrd ones (such as sheet_names(), cellname(row, col), cell_type, cell_value, and so on…) For example, suppose I have the following:

from xlwt import Workbook    
wb = Workbook()
sh1 = wb.add_sheet('sheet1' , cell_overwrite_ok = True)    
sh2 = wb.get_sheet(0)

wb.get_sheet(0) is similar to the rb.sheet_by_index(0) function offered in xlrd, except that the former allows you to modify the contents (provided the user has set cell_overwrite_ok = True)

Assuming xlwt DOES offer the functions I am looking for, I was planning on going through every worksheet again, but this time keeping track of the content that takes the most space for a particular column, and set the column width based on that. Of course, I can also keep track of the max width for a specific column as I write to the sheet, but I feel like it would be cleaner to set the widths after all the data has been already written.

Does anyone know if I can do this? If not, what do you recommend doing in order to adjust the column widths?

  • 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-29T04:58:09+00:00Added an answer on May 29, 2026 at 4:58 am

    I just implemented a wrapper class that tracks the widths of items as you enter them. It seems to work pretty well.

    import arial10
    
    class FitSheetWrapper(object):
        """Try to fit columns to max size of any entry.
        To use, wrap this around a worksheet returned from the 
        workbook's add_sheet method, like follows:
    
            sheet = FitSheetWrapper(book.add_sheet(sheet_name))
    
        The worksheet interface remains the same: this is a drop-in wrapper
        for auto-sizing columns.
        """
        def __init__(self, sheet):
            self.sheet = sheet
            self.widths = dict()
    
        def write(self, r, c, label='', *args, **kwargs):
            self.sheet.write(r, c, label, *args, **kwargs)
            width = arial10.fitwidth(label)
            if width > self.widths.get(c, 0):
                self.widths[c] = width
                self.sheet.col(c).width = width
    
        def __getattr__(self, attr):
            return getattr(self.sheet, attr)
    

    All the magic is in John Yeung’s arial10 module. This has good widths for Arial 10, which is the default Excel font. If you want to write worksheets using other fonts, you’ll need to change the fitwidth function, ideally taking into account the style argument passed to FitSheetWrapper.write.

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

Sidebar

Related Questions

I have been trying to create a regular expressions pattern that matches any reference
I am fairly new to Excel VBA and have been trying to look for
I have automation to create an Excel document from C#. I am trying to
I'm trying to create Excel 2007 Documents programmatically. Now, there are two ways I've
I'm trying to create a new Excel file using jxl, but am having a
I'm trying to create an OleDb connection to an Excel file that is located
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I'm trying to add custom properties to a workbook I have created programmatically. I
I'm trying to create an Excel file with C# COM interop but seems it
I am trying to create a .Net Regex that will separate an Excel header

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.