部署DeepSeek模型,进群交流最in玩法!
立即加群
发布
社区首页 >专栏 >办公利器:DeepSeek+Word教程,让你的工作更高效!

办公利器:DeepSeek+Word教程,让你的工作更高效!

作者头像
Datawhale
发布2025-02-08 13:56:18
发布2025-02-08 13:56:18
10.3K100
代码可运行
举报
文章被收录于专栏:Datawhale专栏Datawhale专栏
运行总次数:0
代码可运行

Datawhale干货 作者:尚启峰,高校行组织者

DeepSeek + Word ,让你的工作更高效!在 Word 界面里,选中文字点击按钮,就能让 DeepSeek 为你快速检索信息、精准翻译文本、智能生成内容等等。这样就不需要在多个软件之间频繁切换,告别低效的信息处理方式,让工作效率大幅提升! 

效果演示

按照文本教程完成操作后,Word 的选项卡中将会出现 DeepSeek 的生成图标,选中文本并点击生成,即可实现模型回复!例如,我们想要根据哪吒 2 番外写一段文稿: 

Image
Image
Image
Image

接下来我将详细介绍,如何实现 DeepSeek 与 Word 和 WPS 的结合。 

获取 API Key

进入 deepseeek 官网链接:https://platform.deepseek.com/sign_in

Image
Image

这样,我们的账号就已经注册好了。接下来我们将使用 deepseek R1 模型。昨天 API 开放平台又可以进入了,免费赠送了 10 元额度。 

Image
Image

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

Image
Image

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

Image
Image
Image
Image

word配置DeepSeek R1

因为这里需要使用到 word 文档中的开发者工具来完成 API 调用,因此我们需要先让开发工具功能显示出来。 

新建一个 Word 文档,点击 文件 -> 选项 -> 自定义功能区,勾选“开发者工具”。 

Image
Image

再点击 信任中心 -> 信任中心设置,选择“启用所有宏”与“信任对VBA......”。 

完成后点击“确定”保存,回到上一菜单后同样点击“确定”保存。这样,“开发工具”就出现在顶部菜单了。 

Image
Image
Image
Image

接下来,我们点击开发者工具,再点击 Visual Basic,将会弹出一个窗口。 

Image
Image

打开后进入的页面是这样。如果中间没有编辑块,则可以点击“插入”,再点击“模块”。 

Image
Image

把以下代码复制进编辑区,再把复制好的密钥放到 api_key = "请输入自己的API密钥" ,替换文本内容。 

代码语言:javascript
代码运行次数:0
复制
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}"
    ' 不想用R1模型,想用V3模型,就把上面的model的deepseek-reasoner换成deepseek-chat

    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 Range

    ' API Key
    api_key = "请输入自己的API密钥"
    If api_key = "" Then
        MsgBox "Please enter the API key.", vbExclamation
        Exit Sub
    End If

    ' 检查是否有选中文本
    If Selection.Type <> wdSelectionNormal Then
        MsgBox "Please select text.", vbExclamation
        Exit Sub
    End If

    ' 保存原始选区
    Set originalSelection = Selection.Range.Duplicate

    ' 处理特殊字符
    inputText = Selection.Text
    inputText = Replace(inputText, "\", "\\")
    inputText = Replace(inputText, vbCrLf, " ")
    inputText = Replace(inputText, vbCr, " ")
    inputText = Replace(inputText, vbLf, " ")
    inputText = Replace(inputText, """", "\""") ' 转义双引号

    ' 发送 API 请求
    response = CallDeepSeekAPI(api_key, inputText)

    ' 处理 API 响应
    If Left(response, 5) <> "Error" Then
        ' 解析 JSON
        Set regex = CreateObject("VBScript.RegExp")
        With regex
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = """content"":""(.*?)""" ' 匹配 JSON 的 "content" 字段
        End With
        Set matches = regex.Execute(response)

        If matches.Count > 0 Then
            ' 提取 API 响应的文本内容
            response = matches(0).SubMatches(0)

            ' 处理转义字符
            response = Replace(response, "\n", vbCrLf)
            response = Replace(response, "\\", "\") ' 处理 JSON 里的反斜杠
            response = Replace(response, "&", "") ' 过滤 `&`,防止意外符号

            ' 让光标移动到文档末尾,防止覆盖已有内容
            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
代码语言:javascript
代码运行次数:0
复制
完成修改后,关闭弹窗即可。 

点击 文件 -> 选项 -> 自定义功能区,右键开发工具,点击添加新组。 

Image
Image

下拉列表找到“宏”,然后把之前创建的宏模块添加到开发工具里: 

Image
Image

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

Image
Image

随后,选中添加的命令,右键点击重命名,选择开始符号作为图标,并重命名为“生成”。

Image
Image

最后点击确定。 

我们发现,工具栏中出现了自己添加的工具。 

Image
Image

至此,Word 成功接入 DeepSeek R1。 

选中文字,点击生成,就可以直接将选中的文本发送给大模型,大模型将会按照你选中的文本,做出响应。 

WPS配置DeepSeek R1

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

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

Image
Image

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

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

Image
Image

目前,官方的 API 现在还不是很稳定

参考资料:

https://mp.weixin.qq.com/s/jocIZQZw0iIpxU5tZGdDiw

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-02-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Datawhale 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Datawhale干货 作者:尚启峰,高校行组织者
  • 效果演示
  • 获取 API Key
  • word配置DeepSeek R1
  • WPS配置DeepSeek R1
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档