How do I import a COM object namespace/enumeration in Python?

前端 未结 4 1173
无人及你
无人及你 2020-12-29 14:07

I\'m relatively new to programming/python, so I\'d appreciate any help I can get. I want to save an excel file as a specific format using Excel through COM. Here is the c

4条回答
  •  借酒劲吻你
    2020-12-29 14:47

    When I used COM to access quickbooks, I could reach the constants defined under a constants member of the object. The code looked something like this (you'll be intersted in the third line):

    self._session_manager.OpenConnection2("",
                                          application_name,
                                          QBFC8Lib.constants.ctLocalQBD)
    

    I'm not sure if this will work, but try this:

    import win32com.client as win32 
    
    def excel():
        app = 'Excel'
        x1 = win32.gencache.EnsureDispatch('%s.Application' % app)
        ss = x1.Workbooks.Add()
        sh = ss.ActiveSheet
        x1.Visible = True
        sh.Cells(1,1).Value = 'test write'
        ss.SaveAs(Filename="temp.xls", FileFormat=x1.constants.xlWorkbookNormal)
        x1.Application.Quit()
    
    if __name__=='__main__':
        excel()
    

    Replace xlWorkbookNormal with whatever format your trying to choose in the X1FileFormat web page you posted in your question.

提交回复
热议问题