如何在 pywin32 中处理 VBA 的关键字参数?
How to handle VBA's keyword arguments in pywin32?
我在尝试在 xlwing 应用程序上使用 pywin32
的 API 函数将特定范围保存为 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
)
我使用了VBA's docs后面的关键字。但是得到了以下回溯:
---------------------------------------------------------------------------
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'
我做错了什么?
您可以尝试使用 IncludeDocProperties = 1
,但 IncludeDocProperties = True
也适用于我的情况,请参阅 here。我试过你的代码,在我的例子中,参数 FileName
不起作用。我必须使用Filename
,不要问我为什么。这是对我有用的代码:
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
)
顺便说一句,你的问题应该是import xlwings as xw
。
我在尝试在 xlwing 应用程序上使用 pywin32
的 API 函数将特定范围保存为 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
)
我使用了VBA's docs后面的关键字。但是得到了以下回溯:
---------------------------------------------------------------------------
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'
我做错了什么?
您可以尝试使用 IncludeDocProperties = 1
,但 IncludeDocProperties = True
也适用于我的情况,请参阅 here。我试过你的代码,在我的例子中,参数 FileName
不起作用。我必须使用Filename
,不要问我为什么。这是对我有用的代码:
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
)
顺便说一句,你的问题应该是import xlwings as xw
。