Page 1 of 1

[Solved] Avoid SAXException dialog with OLE pywin32

Posted: Fri Oct 21, 2022 1:35 pm
by Clorel
I use this python script to convert my file DOCX to PDF with pywin32 in my LibreOffice 7.4.1.2 on Windows 10:
I set a argument for RepairDialog at True, but don't skip the dialog. Do you see, why ?

Other topic like my problem : viewtopic.php?t=39855&hilit=RepairPackage

File with problem :
https://anonfiles.com/ge87FfDdyd/input_docx

Image of Dialog GUI of SaxException :
Image
https://cdn-128.anonfiles.com/Y67aFeD7y7/993a4bc3-1666352690/2022-10-21_13h29_45.png
https://cdn-128.anonfiles.com/Y67aFeD7y7/993a4bc3-1666352690/2022-10-21_13h29_45.png
2022-10-21_13h29_45.png (21.38 KiB) Viewed 15802 times
 Edit: Attached image from anonfiles.com since that link will break. -- MrProgrammer, forum moderator  

My script python :

Code: Select all

def main():
    url_file = "input.docx"
    url_file_temp = "output.pdf"
    def make_property_values(p_obj_service_manager, values):
        return [make_property_value(p_obj_service_manager, value[0], value[1]) for value in values]

    def make_property_value(p_obj_service_manager, name, value):
        o_struct = p_obj_service_manager.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
        o_struct.Name = name
        o_struct.Value = value
        return o_struct

    obj_service_manager = win32com.client.Dispatch("com.sun.star.ServiceManager")
    obj_desktop = obj_service_manager.CreateInstance("com.sun.star.frame.Desktop")
    obj_service_manager._FlagAsMethod("CreateInstance")
    obj_service_manager._FlagAsMethod("Bridge_GetStruct")

    FULL_UPDATE = 3
    ALWAYS_EXECUTE_NO_WARN = 4
    args_opening = make_property_values(obj_service_manager,
                                        [
                                            ["ReadOnly", True],
                                            ["UpdateDocMode", FULL_UPDATE],
                                            ["MacroExecutionMode", ALWAYS_EXECUTE_NO_WARN],
                                            ["RepairPackage", True],
                                            ["Hidden", True]
                                        ])

    document = obj_desktop.loadComponentFromURL(url_file, "_blank", 0, args_opening)
    args_converting = make_property_values(obj_service_manager,
                                           [
                                               ["CompressMode", 1],
                                               ["URL", url_file],
                                               ["FilterName", "writer_pdf_Export"]
                                           ])

    document.storeToUrl(url_file_temp, args_converting)
    obj_desktop.Terminate()
if __name__ == '__main__':
    main()

Re: How to skip SAXException Dialog with OLE pywin32

Posted: Fri Oct 21, 2022 10:52 pm
by JeJe
If you can't stop the dialog appearing you should at least be able to dismiss it without human intervention. I don't know python - If necessary an external program like Autoit might be able to do it. Or you could write your own executable in any language that waits for the window appearing then sends key strokes or a mouseclick to dismiss it.

Re: How to skip SAXException Dialog with OLE pywin32

Posted: Sat Oct 22, 2022 12:34 pm
by ms777
Hi,

the MediaDescriptor passed to the loadComponentFrom Url function allows to specify an XInterActionHandler https://www.openoffice.org/api/docs/com ... ionHandler. Unfortunately, this handler is not called for SAXExceptions.

You probably can go the way JeJe describes. I may also be possible with LO internal programming, but is a lot of work. If you have many corrupt documents you may want to try it. Please note that the importer skips all document content after the first error, which in your case is in the header. If you are interested in more of the docx content, you should try to unzip the docx and repair it by hand

Good luck,

ms777

P.S. it is

Code: Select all

storeToURL
not

Code: Select all

storeToUrl

Re: How to skip SAXException Dialog with OLE pywin32

Posted: Sat Oct 22, 2022 9:26 pm
by Jan_J
The presented dialog box is triggered by exception handler build into OpenOffice Python framework.
Python has ability to handle exceptions using try / except and raise clauses.
However, it this case it seems that at least one among xml files inside docx bundle cannot be parsed by SAX handler. It is interesting, why.