[Solved] Avoid SAXException dialog with OLE pywin32

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
Clorel
Posts: 1
Joined: Fri Oct 21, 2022 1:09 pm

[Solved] Avoid SAXException dialog with OLE pywin32

Post 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 3309 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()
Last edited by MrProgrammer on Wed Feb 28, 2024 7:21 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved] Implement exception handling in Python -- MrProgrammer, forum moderator
LibreOffice 7.4.1.2 on Windows 10
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to skip SAXException Dialog with OLE pywin32

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: How to skip SAXException Dialog with OLE pywin32

Post 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
Jan_J
Posts: 167
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: How to skip SAXException Dialog with OLE pywin32

Post 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.
JJ ∙ https://forum.openoffice.org/pl/
LO (7.6) ∙ Python (3.11|3.10) ∙ Unicode 15 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
Post Reply