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

  • Home
  • SEARCH
  • 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 8832449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:31:52+00:00 2026-06-14T08:31:52+00:00

I’m trying to write and application that creates excel files using python and pywin32

  • 0

I’m trying to write and application that creates excel files using python and pywin32 and I’d like to save the file using the default format and extension for whatever version of excel the user is using. Depending on the version of excel they are using the default format could be “Open XML Workbook” which uses the “.xlsx” extension. Other times it could be the basic excel format and “.xls” extension. Plus the user can configure excel to use some other default format.

I know how to find the default format (Application.DefaultSaveFormat) – but I can’t figure out how to determine the default extension for that format. Part of the problem is that my file names tend to include periods even before the extension:
the basic file name is “filename.BOM” so the actual file name should be “filename.BOM.xls” or “filename.BOM.xlsx” depending on the default format.

If I didn’t have the double period in the file name everything would be fine.
So if the default format is “Open XML Workbook” Workbook.SaveAs( “filename”) would create a file named “filename.xlsx”. But Workbook.SaveAs( “filename.BOM”) creates a file named exactly “filename.BOM”. Excel does not add the default extension when it sees a period already in the file name.

The only thing I can figure out is to save a temporary file, get the extension from that, then delete the temporary file – but that seems really kludgey. Anyone have a better solution?

from tempfile import mktemp
from os import path
from os import remove as delfile
class excel:
    def __init__( self):
        self.app = DispatchEx( "Excel.Application" )
    def saveas_default_ext_format( self, workbook, filename):
        # filename - file name with path but without extension

        tmpname = mktemp()

        alerts = self.app.DisplayAlerts
        self.app.DisplayAlerts = False
        workbook.SaveAs( tmpname)
        self.app.DisplayAlerts = alerts

        tmpname = self.app.ActiveWorkbook.FullName
        x, ext = path.splitext( tmpname)
        fullname = filename + ext
        workbook.SaveAs( fullname)

        delfile( tmpname)

        return fullname
  • 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-06-14T08:31:54+00:00Added an answer on June 14, 2026 at 8:31 am

    Why not make a dict of xlfileformats:extensions and use that to do a lookup:

    from tempfile import mktemp
    from os import path
    from os import remove as delfile
    class excel:
        def __init__( self):
            self.app = DispatchEx( "Excel.Application" )
            self.dct =     {51:'xlsx',
                            52:'xlsm',
                            50:'xlsb',
                            56:'xls'
                            }
    
        def saveas_default_ext_format( self, workbook, filename):
            # filename - file name with path but without extension
    
    
            fullname = '.'.join((filename, self.dct[self.app.DefaultSaveFormat]))
            workbook.SaveAs( fullname)
    
            return fullname
    

    I’ve only included the most common formats in the sample dict, but you can flesh it out from a number of sources on the web, like here. I didn’t put in an KeyError exception handler, but you’ll probably want one.

    good luck,
    Mike

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

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.