DeepSeek可以作为你的office办公好助手

2025-02-15 15:41:35 4点赞 32收藏 1评论

前期准备:

1、进入deepseek的官网,开打API开发平台。

打开官网,进入API开放平台打开官网,进入API开放平台

创建自己的API key。注意:API key只有在创建的时候会显示,一定要保存下来,不然后面无法再看到。API key不能泄露,泄露后所有人都能使用你的key。

创建自己的API key创建自己的API key成功创建API key成功创建API key

2、office word接入API key

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

DeepSeek可以作为你的office办公好助手DeepSeek可以作为你的office办公好助手

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

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

DeepSeek可以作为你的office办公好助手DeepSeek可以作为你的office办公好助手

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

DeepSeek可以作为你的office办公好助手

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

DeepSeek可以作为你的office办公好助手

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


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"":""" & Replace(inputText, """", """") & """}], ""stream"": false}"

' 使用 ServerXMLHTTP 来替代 ADODB.Stream On Error GoTo ErrorHandler Set Http = CreateObject("MSXML2.ServerXMLHTTP") With Http .Open "POST", API, False .SetRequestHeader "Content-Type", "application/json" .SetRequestHeader "Authorization", "Bearer " & api_key

' 打印请求体数据,调试用 Debug.Print "Request Data: " & SendTxt

.Send SendTxt status_code = .Status response = .ResponseText End With

' 打印响应内容,调试用 Debug.Print "Response: " & response

' 如果状态码是200,返回API响应 If status_code = 200 Then CallDeepSeekAPI = response Else MsgBox "Error: " & status_code & " - " & response, vbCritical CallDeepSeekAPI = "Error: " & status_code & " - " & response End If

Set Http = Nothing Exit Function

ErrorHandler: MsgBox "Error occurred: " & Err.Description, vbCritical 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 key,只要key,不要名称。sk-f382e***********************40f8" 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"":s*""(.*?)""" ' 匹配 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


完成修改后,关闭VB弹窗即可。

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

DeepSeek可以作为你的office办公好助手

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

DeepSeek可以作为你的office办公好助手

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

DeepSeek可以作为你的office办公好助手

最后点击确定。

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

DeepSeek可以作为你的office办公好助手

至此,Word 成功接入 DeepSeek R1。

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

展开 收起
1评论

  • 精彩
  • 最新
  • 会生成修改文本吗 ?

    校验提示文案

    提交
提示信息

取消
确认
评论举报

相关文章推荐

更多精彩文章
更多精彩文章

值友2419138557

Ta还没有介绍自己

关注 打赏
最新文章 热门文章
32
扫一下,分享更方便,购买更轻松