DeepSeek or Qwen2.5大模型嵌入Word or WPS 教程

Written by 30115776@qq.com

在日常牛马办公中,人工智能(AI)正逐步成为提升工作效率的重要工具。DeepSeek和Qwen2.5 作为先进的大模型,可以通过 API 集成到 Word or WPS 中,实现智能文本处理。本教程将手把手教你如何在 Word 中嵌入 DeepSeek or Qwen2.5 ,解锁更智能的办公体验。

大模型可以为你快速检索信息、精准翻译文本、智能生成内容等等。这样就不需要在多个软件之间频繁切换,告别低效的信息处理方式,让工作效率大幅提升!

1. 获取 API Key

要使用大模型,首先需要获取 API Key。这里呢,我列举了两个,DeepSeekQwen2.5

接下来,我们以Deepseek 为例,注册账户后,接下来我们将使用 deepseek R1 模型。昨天 API 开放平台又可以进入了,免费赠送了 10 元额度。 

接下来让我们获取 API 密钥,为 word 使用 AI 能力做准备。 

我在这里已经创建了一个 API 密钥了,这步还是非常简单的。创建好后记得复制并保存好自己的密钥,这个作为 API 访问的凭证。 接下来,给word配置DeepSeek。

2. 配置大模型

2.1.1 Word 配置 DeepSeek R1

  1. 打开 Word,点击 文件选项自定义功能区
  2. 勾选 开发者工具,然后点击 确定

2.1.2 配置信任中心

  1. 点击 信任中心信任中心设置
  2. 选择 启用所有宏信任对 VBA 工程对象模型的访问
  3. 完成后点击“确定”保存,回到上一菜单后同样点击“确定”保存。这样,“开发工具”就出现在顶部菜单了。 

2.1.3 添加 VBA 模块

  1. 接下来,我们点击开发者工具,再点击 Visual Basic,将会弹出一个窗口。 
  2. 在 VBA 编辑器中,点击 插入模块
  3. 把以下代码复制进编辑区,再把复制好的密钥放到 api_key = “请输入自己的API密钥” ,替换文本内容。 

2.1.4 自定义功能区

  1. 点击 文件选项自定义功能区
  2. 右键 开发者工具,点击 添加新组
  3. 右键新建组,选择 重命名,将其命名为 DeepSeek 并选择合适的图标。
  4. 选择 DeepSeek(自定义组),在左侧选择 ,添加 DeepSeek
  5. 右键新添加的宏,选择 重命名,改名为 生成 并设置图标。

如果出现下面这样,说明导入成功了。 

2.2 WPS配置DeepSeek R1

没有 Word ,日常使用 WPS 的同学可以看以下增补内容: 

点击“工具”,找到“开发工具”菜单,点击它。 

进入二级菜单后,点击“切换到 VB 环境”,这时候会有提示安装一个插件,点击等待插件安装完成。安装完成后记得要重启 WPS。 

点击“WPS”宏编辑器,剩下的步骤就和 Word 完全一样了。 

3. 使用方法

  1. 在 Word 文档中选中需要处理的文本。
  2. 点击 “生成” 按钮。
  3. 等待 DeepSeek-R1 返回处理结果,即可在文档中查看 AI 生成的内容。

4. 创建模板(可选)

如果需要长期使用 DeepSeek-R1,可以创建一个 Word 模板,让宏每次自动加载。

  1. 点击 文件另存为
  2. 选择 Word 启用宏的模板 (.dotm)
  3. 将模板文件保存到 Word 的启动文件夹(通常是 C:\Users\用户名\AppData\Roaming\Microsoft\Word\STARTUP)。

5. 相关代码

以下是 DeepSeek-R1 在 Word VBA 环境中的代码示例,可直接复制粘贴到 VBA 编辑器中,并替换你的 API Key。

DeepSeekV3 VBA 代码

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

deepseek-reasoner代码

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-reasoner"", ""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 reasoningRegex As Object
    Dim contentRegex As Object
    Dim matches As Object
    Dim reasoningMatches As Object
    Dim originalSelection As Object
    Dim reasoningContent As String
    Dim finalContent As String

    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 reasoningRegex = CreateObject("VBScript.RegExp")
        With reasoningRegex
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = """reasoning_content"":""(.*?)"""
        End With
        
        Set contentRegex = CreateObject("VBScript.RegExp")
        With contentRegex
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = """content"":""(.*?)"""
        End With

        ' 提取推理内容
        Set reasoningMatches = reasoningRegex.Execute(response)
        If reasoningMatches.Count > 0 Then
            reasoningContent = reasoningMatches(0).SubMatches(0)
            reasoningContent = Replace(reasoningContent, "\n\n", vbNewLine)
            reasoningContent = Replace(reasoningContent, "\n", vbNewLine)
            reasoningContent = Replace(Replace(reasoningContent, """", Chr(34)), """", Chr(34))
        End If

        ' 提取最终回答
        Set matches = contentRegex.Execute(response)
        If matches.Count > 0 Then
            finalContent = matches(0).SubMatches(0)
            finalContent = Replace(finalContent, "\n\n", vbNewLine)
            finalContent = Replace(finalContent, "\n", vbNewLine)
            finalContent = Replace(Replace(finalContent, """", Chr(34)), """", Chr(34))

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

            ' 插入推理过程(如果存在)
            If Len(reasoningContent) > 0 Then
                Selection.TypeParagraph
                Selection.TypeText "推理过程:"
                Selection.TypeParagraph
                Selection.TypeText reasoningContent
                Selection.TypeParagraph
                Selection.TypeText "最终回答:"
                Selection.TypeParagraph
            End If

            ' 插入最终回答
            Selection.TypeText finalContent

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

qwen2.5-max代码

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

总结

通过本教程,你已经学会了如何在 Word 中嵌入 DeepSeek or Qwen 大模型,实现智能文本处理、翻译等功能。这种方法不仅可以提高办公效率,还能为日常文档编辑带来更便捷的体验。如果你有更高级的需求,例如使用 DeepSeekQwen2.5-max 进行更复杂的文本推理,可以按照类似的方法修改代码。

30115776@qq.com

Pretium lorem primis lectus donec tortor fusce morbi risus curae. Dignissim lacus massa mauris enim mattis magnis senectus montes mollis taciti accumsan semper nullam dapibus netus blandit nibh aliquam metus morbi cras magna vivamus per risus.

关于2025年的思考:未来职业怎么选?AI、数据、新能源可能是香饽饽

AstrBot:轻松将大模型接入QQ、微信等消息平台,打造多功能AI聊天机器人开发框架【附详细教程】

发表评论