How get list of Databases using Dispatch('ADODB.Connection')? - python

I am using PIOLEDBENT provider, i need to extract list of databases in my PIAF Server when i call oConn.GetSchema i throws exception mention bellow. please guide me what is wrong with my code.
from win32com.client import Dispatch
oConn = Dispatch('ADODB.Connection')
oRS = Dispatch('ADODB.RecordSet')
oConn.ConnectionString = "Provider=PIOLEDBENT; Data Source=localhost; Integrated Security=True; Integrated Security=True;User ID=abubakr;Password=#Spark123!"
oConn.Open()
oConn.GetSchema();
Exception :
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'ADODB.Connection', u'Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.', u'C:\\Windows\\HELP\\ADO270.CHM', 1240641, -2146825287), None)

Related

com_error: When converting docx to pdf in python

I am currently trying to convert some word documents to PDF using python, some of the files converted successfully with the code I have while I got the following error messages on other files:
Error 1: some files reported
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'String is longer than 255 characters', 'wdmain11.chm', 41873, -2146819183), None)
Error 2: Some reported
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'Command failed', 'wdmain11.chm', 36966, -2146824090), None)
Error 3: Some reported
AttributeError: 'NoneType' object has no attribute 'SaveAs'
Below is the code I am using, any idea why this is happening:
from docx2pdf import convert
def convert_word_to_pdf(word_path):
pdf_path = word_path.replace(".docx", ".pdf")
convert(word_path, pdf_path)
word_path = '*file_path*'
convert_word_to_pdf(word_path)
Thank you for your support
I expected the python code above to convert all the files to PDF.

Python pywin32 error passing an optional string via a VBA click

I am working with legacy access code and am using pywin32 to automate batch processing.
This python code:
import win32com.client as win32
access = win32.Dispatch('Access.Application')
db = access.OpenCurrentDatabase(r'C:\test\PN.mdb')
access.Forms('frm_PN_Startup').cmdScenarioExport_Click()
sucessfully executes this Access VBA sub
Public Sub cmdScenarioExport_Click()
Export_Scenario_To_ScenarioDB
End Sub
I would like to pass an optional text string to fill the Access InputBox that opens in Export_Scenario_To_ScenarioDB. However, when I try
Python
import win32com.client as win32
access = win32.Dispatch('Access.Application')
db = access.OpenCurrentDatabase(r'C:\test\PN.mdb')
access.Forms('frm_PN_Startup').cmdScenarioExport_Click("hello")
Vba
Public Sub cmdScenarioExport_Click(Optional strTest As String = "")
Export_Scenario_To_ScenarioDB (strTest)
End Sub
I get the following error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\RS\miniconda3\envs\PN_PY\lib\site-packages\win32com\client\dynamic.py", line 197, in __call__
return self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.defaultDispatchName,None)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146788252), None)
win32api tells me that the error is
win32api.FormatMessage(-2147352567)
'Exception occurred.\r\n'
After numerous hours spinning my wheels, I'm asking for any ideas to make this work. Thank you! This is my first foray into VBA.

MS ACCESS Compact and Repair using Python is giving the error file can not open exclusively

I tried the following script but that is giving me the error
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2146959355, 'Server execution failed', None, None)
objAccess = win32com.client.Dispatch("Access.application")
objAccess.compactRepair(srcDB, destDB)
Can anybody help me with this please.
To open the database in exclusive mode, you need to change "/" to "backslash" \ in the path:
path_bdd_access = path_bdd_access.replace('/', chr(92))

Not able to read a macro in python

I am trying to read a macro in excel through python code but not able to do so as it is not able to find the macro. Also apart from the code if I try to view the macro just by opening Excel > Alt + F11 then I am supposed to enter the password. Is this the reason that the python code is not able to read the macro as I am trying to read a password protected macro using python. If so, please suggest what should be the workaround to read it. Thanks! Attaching the code snippet and error below :
Code :
import os
import win32com.client
os.chdir("C:\new folder\Input folder")
if os.path.exists("Input excel.xlsm"):
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(os.path.abspath("Input excel.xlsm"))
xl.Application.Run("Input excel.xlsm!module1.mymethod") #mymethod is of type sub
xl.Application.Save()
xl.Application.Quit()
del xl
Error :
result = self._oleobj_.InvokeTypes(*(dispid, LCID, wFlags, retType, argTypes) + args)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u"Cannot run the macro 'Input excel.xlsm!module1.mymethod'. The macro may not be available in this workbook or all macros may be disabled.", u'xlmain11.chm', 0, -2146827284), None)
I have tried all the below possible solutions but the issue is still there :
1) Using the correct convention to call the macro : xl.Application.Run("excelsheet.xlsm!modulename.macroname")
2) Changing the excel settings :
1.File > Options > Trust Center
2.Click on Trust Center Settings... button
3.Macro Settings > Check Enable all macros

Using MAPI to retrieve the list of emails from GAL

This is my code so far:
import win32com.client
o = win32com.client.gencache.EnsureDispatch("Outlook.Application")
ns = o.GetNamespace("MAPI")
adrLi = ns.AddressLists.Item("Global Address List")
contacts = adrLi.AddressEntries
numEntries = adrLi.AddressEntries.Count
nameAliasDict = {}
for i in contacts:
name = i.Name
alias = i.Address.split("=")[-1]
print i.GetExchangeUser().PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3A56101E")
I"m getting the property from: https://msdn.microsoft.com/en-us/library/bb446002.aspx
But for some reasons, I get this error:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u'The property "http://schemas.microsoft.com/mapi/proptag/0x3A550003" is unknown or cannot be found.', None, 0, -2147221233), None)
Am I doing this wrong?
You cannot assume that PR_CONTACT_EMAIL_ADDRESSES or any other MAPI property will be available. Your code must expect and handle that error from PropertyAccessor.GetProperty.
Check if you can actually see the property on that particular object in OutlookSpy (I am its author - click IAddrBook, "Open Root Container" etc.).
Why exactly do you need that property? If you just need the SMTP address. use ExchangeUser.PrimarySmtpAddress.

Categories