Search code examples
pythonpywin32xlwings

How to handle VBA's keyword arguments in pywin32?


I'm having an issue trying to use a pywin32's API function on a xlwing aplication for saving a certain Range as a pdf:

import xlwings as sw

app  = xw.App(add_book=False, visible=False)
book = app.books.open(fullname=GERALAMINA,
                    update_links=True,
                    read_only=False,
                    ignore_read_only_recommended=True)

sht = book.sheets[0]
sht.range('A1:B10').api.ExportAsFixedFormat(
            Type                 = 0, # xlTypePDF
            FileName             = r'C:/some/path/filename.pdf',
            Quality              = 0, # xlQualityStandard
            IncludeDocProperties = True
        )

I used the keywords following VBA's docs. But got the following traceback:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-66-dece9e682dc6> in print_lamina(self)
    345             FileName             = r'C:/some/path/filename.pdf',
    346             Quality              = 0, # xlQualityStandard
--> 347             IncludeDocProperties = True
    348         )
    349 

~\AppData\Roaming\Python\Python37\site-packages\xlwings\_xlwindows.py in __call__(self, *args, **kwargs)
     64         while True:
     65             try:
---> 66                 v = self.__method(*args, **kwargs)
     67                 if isinstance(v, (CDispatch, CoClassBaseClass, DispatchBaseClass)):
     68                     return COMRetryObjectWrapper(v)

TypeError: ExportAsFixedFormat() got an unexpected keyword argument 'FileName'

What am I doing wrong?


Solution

  • You can try it with IncludeDocProperties = 1 but IncludeDocProperties = True also works in my case, see here. I tried your code and in my case the parameter FileName did not work. I have to use Filename, don't ask me why. Here is the code that works for me:

    import xlwings as xw
    
    app  = xw.App(add_book=False, visible=False)
    book = app.books.open(fullname=GERALAMINA,
                        update_links=True,
                        read_only=False,
                        ignore_read_only_recommended=True)
    
    sht = book.sheets[0]
    sht.range('A1:B10').api.ExportAsFixedFormat(
                Type                 = 0, # xlTypePDF
                Filename             = r'C:/some/path/filename.pdf',
                Quality              = 0, # xlQualityStandard
                IncludeDocProperties = 1
    )
    

    By the way it, should be import xlwings as xw in your question.