优秀的编程知识分享平台

网站首页 > 技术文章 正文

让你把DeepSeek不借助插件装进WPS中 WPS接入DeepSeek人工智能

nanyue 2025-05-26 17:47:03 技术文章 9 ℃


2025 年春节,Deepseek 那可真是大火特火,出了圈啦!WPS 呢,作为一种文本处理的工具,它自身是带有 AI 功能的,不过要是想用这个 AI 功能,就得开通会员。Deepseek 呢,是个特别厉害的搜索和信息提取的工具,在好多场景里都能起到重要作用。这篇文章会给您讲讲,在不用任何额外插件的情况下,通过一些简简单单的步骤,把 Deepseek 的功能弄到 WPS Office 里,这样就能让平常办公的效率更高啦。

使用效果:

选择文字,点击功能菜单中的生成,即可实现和Deepseek的对话,并且让回复的文字直接进入WPS中,不需要切换应用。如下图:




实现步骤:

  1. 准备API key

获取地址:

- deepseek-r1模型API Key获取地址:https://www.deepseek.com/

- qwen2.5模型API Key获取地址:
https://bailian.console.aliyun.com/

(附由于当前的deepseek很火,API可能无法访问,可以使用千问的作平替)

  1. 配置WPS

(1)配置信任中心,启用宏。详细步骤:找到WPS的“选项”对话框,找到“信息中心”,勾选“启用宏.....”


(2)增加模块

详细步骤:找到“开发工具”,选择“VB编辑器”,打开新的窗口


选择“插入”菜单,找到“模块”

打开模块窗口,输入代码(附后)

特别提醒:将代码复制到编辑区中(**注意替换你自己的API key**)

将以上操作完成后关闭VB编辑窗口。

(3)设置功能菜单

进入“选项”对话框,找到“自定义功能区”,在右侧主选项卡的“开始”,选择下面的按钮“新建组”,这里名称可以自定义。我输入的是“AI智能”

选择刚新添加的“新建组”,找到左侧的命令区,选择“宏”找到刚才添加的宏模块,然后添加即可。(注:这里可以根据需要修改名称)


完成以上的操作,进入WPS文字之后,在“开始”选项卡,最后就会出现一个刚才添加的模块。

  1. 使用方法:

选中需要处理的文字,点击刚才自定义的按钮,等待大模型响应。(此时响应的过程是有点慢,请耐心等待)


(由于DeepSeek的KEY暂时不能使用,这里使用了通义千问)

附件:

DeepSeek V3代码:

Function CallDeepSeekAPI(api_key As String, inputText As String) As String
    Dim API As String
    Dim SendTxt As String
    Dim Http As Object
    Dim status_code As Integer
    Dim response As String

    API = "https://api.deepseek.com/chat/completions"
    SendTxt = "{""model"": ""deepseek-chat"", ""messages"": [{""role"":""system"", ""content"":""You are a Word assistant""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": false}"

    Set Http = CreateObject("MSXML2.XMLHTTP")
    With Http
        .Open "POST", API, False
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "Authorization", "Bearer " & api_key
        .send SendTxt
        status_code = .Status
        response = .responseText
    End With

    ' 弹出窗口显示 API 响应(调试用)

    ' MsgBox "API Response: " & response, vbInformation, "Debug Info"

    If status_code = 200 Then
        CallDeepSeekAPI = response
    Else
        CallDeepSeekAPI = "Error: " & status_code & " - " & response
    End If

    Set Http = Nothing
End Function

Sub DeepSeekV3()
    Dim api_key As String
    Dim inputText As String
    Dim response As String
    Dim regex As Object
    Dim matches As Object
    Dim originalSelection As Object

    api_key = "替换为你的api key"
    If api_key = "" Then
        MsgBox "Please enter the API key."
        Exit Sub
    ElseIf Selection.Type <> wdSelectionNormal Then
        MsgBox "Please select text."
        Exit Sub
    End If

    ' 保存原始选中的文本
    Set originalSelection = Selection.Range.Duplicate

    inputText = Replace(Replace(Replace(Replace(Replace(Selection.text, "\", "\\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), "\""")
    response = CallDeepSeekAPI(api_key, inputText)

    If Left(response, 5) <> "Error" Then
        Set regex = CreateObject("VBScript.RegExp")
        With regex
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = """content"":""(.*?)"""
        End With
        Set matches = regex.Execute(response)
        If matches.Count > 0 Then
            response = matches(0).SubMatches(0)
            response = Replace(Replace(response, """", Chr(34)), """", Chr(34))

            ' 取消选中原始文本
            Selection.Collapse Direction:=wdCollapseEnd

            ' 将内容插入到选中文字的下一行
            Selection.TypeParagraph ' 插入新行
            Selection.TypeText text:=response

            ' 将光标移回原来选中文本的末尾
            originalSelection.Select
        Else
            MsgBox "Failed to parse API response.", vbExclamation
        End If
    Else
        MsgBox response, vbCritical
    End If
End Sub

通义千问Max2.5 代码:

Function CallDeepSeekAPI(api_key As String, inputText As String) As String
    Dim API As String
    Dim SendTxt As String
    Dim Http As Object
    Dim status_code As Integer
    Dim response As String

    API = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
    SendTxt = "{""model"": ""qwen-max-0125"", ""messages"": [{""role"":""system"", ""content"":""You are a Word assistant""}, {""role"":""user"", ""content"":""" & inputText & """}]}"

    Set Http = CreateObject("MSXML2.XMLHTTP")
    With Http
        .Open "POST", API, False
        .setRequestHeader "Content-Type", "application/json"
        .setRequestHeader "Authorization", "Bearer " & api_key
        .send SendTxt
        status_code = .Status
        response = .responseText
    End With

    ' 弹出窗口显示 API 响应(调试用)
    ' MsgBox "API Response: " & response, vbInformation, "Debug Info"

    If status_code = 200 Then
        CallDeepSeekAPI = response
    Else
        CallDeepSeekAPI = "Error: " & status_code & " - " & response
    End If

    Set Http = Nothing
End Function

Sub DeepSeekV3()
    Dim api_key As String
    Dim inputText As String
    Dim response As String
    Dim regex As Object
    Dim matches As Object
    Dim originalSelection As Object

    api_key = "替换为你的api key"
    If api_key = "" Then
        MsgBox "Please enter the API key."
        Exit Sub
    ElseIf Selection.Type <> wdSelectionNormal Then
        MsgBox "Please select text."
        Exit Sub
    End If

    ' 保存原始选中的文本
    Set originalSelection = Selection.Range.Duplicate

    inputText = Replace(Replace(Replace(Replace(Replace(Selection.text, "\", "\\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), "\""")
    response = CallDeepSeekAPI(api_key, inputText)

    If Left(response, 5) <> "Error" Then
        Set regex = CreateObject("VBScript.RegExp")
        With regex
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = """content"":""(.*?)"""
        End With
        Set matches = regex.Execute(response)
        If matches.Count > 0 Then
            response = matches(0).SubMatches(0)
            ' 处理换行符和特殊字符
            response = Replace(response, "\n\n", vbNewLine)  ' 双换行替换为真实的单换行
            response = Replace(response, "\n", vbNewLine)  ' 单换行替换为真实的换行
            response = Replace(Replace(response, """", Chr(34)), """", Chr(34))

            ' 取消选中原始文本
            Selection.Collapse Direction:=wdCollapseEnd

            ' 将内容插入到选中文字的下一行
            Selection.TypeParagraph ' 插入新行
            Selection.TypeText text:=response

            ' 将光标移回原来选中文本的末尾
            originalSelection.Select
        Else
            MsgBox "Failed to parse API response.", vbExclamation
        End If
    Else
        MsgBox response, vbCritical
    End If
End Sub

Tags:

最近发表
标签列表