com_error: When converting docx to pdf in python - 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.

Related

Open Method of Workbooks Class Failed

File "C:\PythonPlus\64it\envs\Python27\lib\site-packages\win32com\gen_py\00020813-0000-C000-000000000046x0x1x9\Workbooks.py", line78,in open
, Converter, AddToMru, Local,CorruptLoad)
com_error: (-2147352567, 'exception occurred.', (0, u'microsoft excel', u'open method of workbooks class failed', u'xlmain11.chm', 0, -2146827284), none)
I am getting above error.
when I try to open the xml file, it says strict parse error.
How to correct it?
I m expecting it to open the xml file.

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

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

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)

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