import os import sys if False: from PyQt5.QtWidgets import QApplication from PyQt5.QAxContainer import QAxWidget else: from PySide2.QtWidgets import QApplication from PySide2.QtAxContainer import QAxWidget from PySide2.QtCore import QLibraryInfo if __name__ == '__main__': app = QApplication(sys.argv) opt_single = "-s" in sys.argv print(QLibraryInfo.build()) excel = QAxWidget('Excel.Application') if excel.isNull(): print('Not available') sys.exit(1) excel.setProperty('Visible', True) workbooks = excel.querySubObject('WorkBooks') if workbooks.isNull(): print('workbooks not available') sys.exit(1) workbooks.dynamicCall('Add') workbook = excel.querySubObject('ActiveWorkBook') if workbook.isNull(): print('workbook not available') sys.exit(1) help(QAxWidget.dynamicCall) # PySide2 gives: sig = 'SaveAs(QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant, QVariant)' # SaveAs (FileName, FileFormat, Password, WriteResPassword, ReadOnlyRecommended, CreateBackup, AccessMode, ConflictResolution, AddToMru, TextCodepage, TextVisualLayout, Local) args = [ os.path.join(os.getcwd(), 'filename.xlsx'), 51, '', '', False, False, 1, 2, False, False, False, False, ] # 1) too many arguments error, due to: https://doc.qt.io/qt-5/qaxbase.html#dynamicCall # workbook.dynamicCall(sig, *args) # 2) unexpected behavior, works as if it was: # # for arg in args: # workbook.dynamicCall(sig, arg) # # but expected to behave like: https://doc.qt.io/qt-5/qaxbase.html#dynamicCall-1 if opt_single: print('>single') workbook.dynamicCall(sig, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]) print('list') # recursion bug workbook.dynamicCall(sig, args) print('