pywpsrpc

repository·master·Indexed 43 days ago

https://github.com/timxx/pywpsrpc

Python bindings for the C++ secondary development interfaces of WPS Office for Linux. It provides modules to automate WPS Writer (rpcwpsapi), WPS Presentation (rpcwppapi), and WPS Spreadsheets (rpcetapi), allowing developers to manipulate documents, register events, embed WPS into custom windows, and convert files to PDF.

Tokens
2.5K
Snippets
11
Records
15
Agent score
55%

What's inside pywpsrpc

  1. Identify the correct module for WPS, WPP, or ET

    master

    The library provides specific modules depending on which WPS Office component you want to control via RPC:

    • rpcwpsapi: Interfaces for WPS Writer (WPS).
    • rpcwppapi: Interfaces for WPS Presentation (WPP).
    • rpcetapi: Interfaces for WPS Spreadsheets (ET).
    • common: Contains shared interfaces used by the others; it cannot be used independently.
  2. Handle HRESULT return values

    master

    Most WPS development interface calls return an HRESULT as the first value in a tuple.

    • A return value of 0 (equivalent to common.S_OK) indicates success.
    • Any non-zero value typically indicates a failure.
    • You can use the FAILED or SUCCEEDED helpers from the common module to evaluate these results.
  3. Understand the pywpsrpc module structure

    master

    The library is divided into specific modules based on the WPS Office component you wish to automate:

    • rpcwpsapi: Interface for WPS Writer (WPS 文字).
    • rpcwppapi: Interface for WPS Presentation (WPS 演示).
    • rpcetapi: Interface for WPS Spreadsheets (WPS 表格).
    • common: Contains shared interfaces used by the three modules above. Note that common is typically not intended for standalone use.

    To interact with WPS, you must use the createXXXRpcInstance function provided by the specific component module.

  4. Quickstart: Automate WPS Writer

    master

    This example demonstrates how to initialize a WPS Writer instance, create a document, manipulate text, register an event, and save the file.

    from pywpsrpc.rpcwpsapi import (createWpsRpcInstance, wpsapi)
    from pywpsrpc import RpcIter
    
    # 1. Create RPC instance
    hr, rpc = createWpsRpcInstance()
    
    # 2. Launch WPS Application
    hr, app = rpc.getWpsApplication()
    
    # 3. Create a new document
    hr, doc = app.Documents.Add()
    
    # 4. Manipulate text
    selection = app.Selection
    selection.InsertAfter("Hello, world")
    selection.Font.Bold = True
    selection.EndKey()
    selection.InsertParagraph()
    selection.MoveDown()
    selection.TypeText("pywpsrpc~")
    
    # 5. Register an event (e.g., DocumentBeforeSave)
    def onDocumentBeforeSave(doc):
        print("onDocumentBeforeSave called for doc: ", doc.Name)
        # Return (Cancel, SaveAsUI). Returning (True, False) cancels the save.
        return True, False
    
    rpc.registerEvent(app,
                      wpsapi.DIID_ApplicationEvents4,
                      "DocumentBeforeSave",
                      onDocumentBeforeSave)
    
    # 6. Save and Quit
    doc.SaveAs2("test.docx")
    app.Quit(wpsapi.wdDoNotSaveChanges)
  5. Install pywpsrpc

    master

    You can install pywpsrpc directly from PyPI using pip.

    Requirements:

    • Python 3.6+
    • Qt5
    • WPS Office for Linux 11.1.0.9080+
    • A Desktop Environment (WPS Office must be installed)

    Note: If you are embedding WPS into your own program, you should switch WPS to All-in-One mode.

    pip install pywpsrpc
  6. Convert WPS Spreadsheets to PDF using et_convert.py

    master

    You can use the et_convert.py script to convert WPS Spreadsheet files (.xlsx, .xls) into PDF format via the command line.

    Usage: Run the script with the --format flag set to "pdf" followed by the path to your spreadsheet file.

    Example:

    et_convert.py --format "pdf" test.xlsx
  7. Convert WPS Presentation (PPTX/PPT) to PDF

    master

    Use the wpp_convert.py script to convert WPS Presentation files (.pptx or .ppt) into PDF format. This is an example implementation using the rpcwppapi module.

    Usage: Run the script from the command line, specifying the target --format and the input file path.

    wpp_convert.py --format "pdf" test.pptx
  8. Quick Start: Automate WPS Writer

    master

    This example demonstrates how to initialize the RPC instance, create a new document, manipulate text, iterate through paragraphs, and register an event handler in WPS Writer.

    Key Concepts:

    • Error Handling: Check the first return value of calls (0 is success).
    • Iteration: Use RpcIter to iterate over collections like Paragraphs.
    • Collection Access: Supports index-based access (1-based for some collections) and VBA-style call syntax collection(index).
    • Events: Use rpc.registerEvent to bind Python functions to WPS application events.
    from pywpsrpc.rpcwpsapi import (createWpsRpcInstance, wpsapi)
    from pywpsrpc import RpcIter
    
    # 1. Create the rpc instance
    hr, rpc = createWpsRpcInstance()
    
    # 2. Get the application and create a document
    hr, app = rpc.getWpsApplication()
    hr, doc = app.Documents.Add()
    
    # 3. Manipulate text via Selection
    selection = app.Selection
    selection.InsertAfter("Hello, world")
    selection.Font.Bold = True
    selection.EndKey()
    selection.InsertParagraph()
    selection.TypeText("pywpsrpc~")
    
    # 4. Iterate through paragraphs
    paras = doc.Paragraphs
    for para in RpcIter(paras):
        print(para.Range.Text)
    
    # VBA-style access (1-based index)
    first_para = paras(1)
    print(first_para.Range.Text)
    
    # 5. Register an event handler
    def onDocumentBeforeSave(doc):
        print("onDocumentBeforeSave called for doc: ", doc.Name)
        # Return (SaveAsUI, Cancel). Return True, False to allow save.
        return True, False
    
    rpc.registerEvent(app,
                      wpsapi.DIID_ApplicationEvents4,
                      "DocumentBeforeSave",
                      onDocumentBeforeSave)
    
    # 6. Save and Quit
    doc.SaveAs2("test.docx")
    app.Quit(wpsapi.wdDoNotSaveChanges)
  9. Convert documents to a specified format using convertto.py

    master

    Use the convertto.py script to convert WPS documents into different formats (e.g., PDF). The script accepts a --format flag followed by the target format and one or more file paths. The converted files are generated in an out directory.

    Usage: python convertto.py --format "<format>" "<file_path_1>" "<file_path_2>" ...

    python convertto.py --format "pdf" README.md