def _startsas(self) -> str: """ Create a workspace and open a connection with SAS. :return [str]: """ if getattr(self, 'workspace', None) is not None: # Do not create a new connection return self.workspace.UniqueIdentifier factory = dynamic.Dispatch('SASObjectManager.ObjectFactoryMulti2') server = dynamic.Dispatch('SASObjectManager.ServerDef') self.keeper = dynamic.Dispatch('SASObjectManager.ObjectKeeper') self.adodb = dynamic.Dispatch('ADODB.Connection') if self.sascfg.host is None: # Create a local connection. server.MachineDNSName = '127.0.0.1' server.Port = 0 server.Protocol = self.PROTOCOL_COM user = None password = None else: # Create a remote connection. The following are required: # 1. host # 2. port # 3. class_id server.MachineDNSName = self.sascfg.host server.Port = self.sascfg.port server.Protocol = self.PROTOCOL_IOM server.ClassIdentifier = self.sascfg.class_id if self.sascfg.user is not None: user = self.sascfg.user else: user = self.sascfg._prompt('Username: '******'Password: '******'WorkspaceObject', self.workspace) self.adodb.Open('Provider={}; Data Source=iom-id://{}'.format( self.sascfg.provider, self.workspace.UniqueIdentifier)) ll = self.submit( "options svgtitle='svgtitle'; options validvarname=any validmemname=extend pagesize=max nosyntaxcheck; ods graphics on;", "text") if self.sascfg.verbose: print( "SAS Connection established. Workspace UniqueIdentifier is " + str(self.workspace.UniqueIdentifier) + "\n") return self.workspace.UniqueIdentifier
def _startsas(self) -> str: """ Create a workspace and open a connection with SAS. :return [str]: """ if getattr(self, 'workspace', None) is not None: # Do not create a new connection return self.workspace.UniqueIdentifier factory = dynamic.Dispatch('SASObjectManager.ObjectFactoryMulti2') server = dynamic.Dispatch('SASObjectManager.ServerDef') self.keeper = dynamic.Dispatch('SASObjectManager.ObjectKeeper') self.adodb = dynamic.Dispatch('ADODB.Connection') if self.sascfg.host is None: # Create a local connection. server.MachineDNSName = '127.0.0.1' server.Port = 0 server.Protocol = self.PROTOCOL_COM user = None password = None else: # Create a remote connection. The following are required: # 1. host # 2. port # 3. class_id server.MachineDNSName = self.sascfg.host server.Port = self.sascfg.port server.Protocol = self.PROTOCOL_IOM server.ClassIdentifier = self.sascfg.class_id if self.sascfg.user is not None: user = self.sascfg.user else: user = self.sascfg._prompt('Username: '******'Password: '******'WorkspaceObject', self.workspace) self.adodb.Open('Provider={}; Data Source=iom-id://{}'.format( self.sascfg.provider, self.workspace.UniqueIdentifier)) return self.workspace.UniqueIdentifier
def get_active_workbook(): from win32com.client import dynamic com_app = dynamic.Dispatch("Excel.Application") com_wb = com_app.ActiveWorkbook wb = xw.Workbook(xl_workbook=com_wb) return wb
def _get_active_workbook(): from win32com.client import dynamic #@UnusedImport com_app = dynamic.Dispatch('Excel.Application') #@UndefinedVariable com_wb = com_app.ActiveWorkbook wb = xw.Workbook(xl_workbook=com_wb) return wb
def _get_latest_app(): """ Only dispatch Excel if there isn't an existing application - this allows us to run open_workbook() and new_workbook() in the correct Excel instance, i.e. in the one that was instantiated last. Otherwise it would pick the application that appears first in the Running Object Table (ROT). """ try: return xl_workbook_current.Application except (NameError, pywintypes.com_error): return dynamic.Dispatch('Excel.Application')
def check_excell_installed(): """Checks once and returns `True` if Excel-app is installed in the system.""" global _xl_installed if _xl_installed is None: try: from win32com.client import dynamic # @UnresolvedImport dynamic.Dispatch("Excel.Application") _xl_installed = True except Exception: # pragma: no cover _xl_installed = False return _xl_installed
def read_sasdata(self, table: str, libref: str = None, dsopts: dict = None) -> tuple: """ Read any SAS dataset and return as a tuple of header, rows :param table [str]: Table name :option libref [str]: Library name. :option dsopts [dict]: Dataset options. :return [tuple]: """ TARGET = '_saspy_sd2df' EXPORT = """ data {tgt}; set {tbl} {dopt}; run; """ dsopts = self._sb._dsopts(dsopts) if dsopts is not None else '' tablepath = self._sb._tablepath(table, libref=libref) recordset = dynamic.Dispatch('ADODB.RecordSet') # Create an intermediate dataset with `dsopts` applied export = EXPORT.format(tgt=TARGET, tbl=tablepath, dopt=dsopts) self.workspace.LanguageService.Submit(export) meta = self._schema(TARGET) # Connect RecordSet object to ADODB connection with params: # Cursor: Forward Only # Lock: Read Only # Command: Table Direct recordset.Open(TARGET, self.adodb, self.CURSOR_FORWARD, self.LOCK_READONLY, self.CMD_TABLE_DIRECT) recordset.MoveFirst() header = [x.Name for x in recordset.Fields] rows = [] while not recordset.EOF: rows.append( [meta[x.Name]['CONVERT'](x.Value) for x in recordset.Fields]) recordset.MoveNext() recordset.Close() return (header, rows)
def __init__(self, visible=True): """ :param visible: Catia's window visible or not :type visible: bool """ self.parent = None self.cat_constructor = dynamic.Dispatch("CATIA.Application") self.cat_constructor.Visible = visible self.name = self.cat_constructor.Name self.parentsDict = OrderDict() self.parentsDict[self.name] = self self.documents_COLL = Documents(self.parentsDict._copy()) self.windows_COLL = Windows(self.parentsDict._copy()) caller = inspect.getouterframes(inspect.currentframe())[1][1] caller = re.findall('([a-z]*)\.py.*', caller)[0] if caller in ['part', 'product', 'drawing']: print('Not create Document') pass else: self.documents_COLL.add('Part') pass
def getOpenedWorkbooksNames(): com_app = dynamic.Dispatch('Excel.Application') com_wbs = com_app.Workbooks wb_names = [wb.Name for wb in com_wbs] return wb_names
import win32com.client.dynamic as win32client explorer = win32client.Dispatch("InternetExplorer.Application") explorer.Visible = True explorer.Quit() word = win32client.Dispatch('Word.Application') word.Visible = True word.Quit() excel = win32client.Dispatch("Excel.Application") excel.Interactive = False excel.Visible = True wb = excel.Workbooks.Add() ws = wb.Worksheets("Sheet1") ws.Cells(1, 1).Value = 'hello world' wb.SaveAs( r'C:\Users\HY\Desktop\TradingPractice\trading_practice\result_file\text.xlsx' ) excel.Quit() wb = excel.Workbooks.Open( r'C:\Users\HY\Desktop\TradingPractice\trading_practice\result_file\text.xlsx' ) ws = wb.ActiveSheet print(ws.Cells(1, 1).Value) ws.Cells(1, 2).Value = 'is' ws.Range('C1').Value = 'good' ws.Range('C1').Interior.ColorIndex = 10 ws.Range('A1:F4').Interior.ColorIndex = 11
def new_workbook(): xl_app = dynamic.Dispatch('Excel.Application') xl_app.Visible = True xl_workbook = xl_app.Workbooks.Add() return xl_app, xl_workbook
def open_workbook(fullname): xl_app = dynamic.Dispatch('Excel.Application') xl_workbook = xl_app.Workbooks.Open(fullname) xl_app.Visible = True return xl_app, xl_workbook