[Python] Open Word document as "Read Only" with API doesn't work

Discussão em 'Python' iniciado por Stack, Maio 9, 2025.

  1. Stack

    Stack Membro Participativo

    I tried to use Python to open a Word file using the API, and the Word file opens as usual. When I open it as read-only from Word, it works as expected.

    API reference: https://learn.microsoft.com/en-us/office/vba/api/word.documents.open

    Example code:

    import win32com.client as win32

    filename="C:\\temp\\test.docx"

    try:
    word = win32.Dispatch('Word.Application')
    except AttributeError:
    print("Failing connecting with Word...")
    exit()
    except Exception as e:
    print('Unknown Exception: {0}.'.format(e))
    exit()

    if word is None:
    print("Word could not be opened. Cancelling request")

    try:
    retval = word.Documents.Open(filename, ReadOnly=True, Visible=True)
    word.Visible = True
    word.Documents[word.Documents.count-1].Activate()
    word.Activate()
    except Exception as e:
    print('Unknown Exception: {0}.'.format(e))
    exit()


    any ideas?

    Continue reading...

Compartilhe esta Página