[项目实践] 把DeepSeek接入Word软件,给工作提质增效!

572 0
Honkers 2025-3-5 17:31:27 | 显示全部楼层 |阅读模式

前几天给大家分享了 DeepSeek 的资源包,可能很多人并没有本地部署 DeepSeek 的需求,只想使用它来提高一下工作效率。那今天来分享一下怎么直接在 Word 软件调用 DeepSeek,避免在 Word 软件和网页版 DeepSeek 里来回切换。

## 前置条件

1、有外网,通过 API 调用 DeepSeek。

2、Office 软件中的 Word,笔者是 Office 365。

3、有 DeepSeek 的 API key,这个可以免费在官网申请。官网地址:https://www.deepseek.com

在官网右上角有个“API 开发平台”的按钮,点进去再选左侧的 “API keys” 按提示就可以免费注册 API key 了。

很不幸的是,由于最近泼天的流量,官网已经暂停充值,连新用户的 10 元赠送余额也没有了。主要还是因为最近访问量太大了,等着官网再次开放。

创建的 API key 一定要记在记事本上保存好,关闭后就没法再次查看,只能再申请新的 API key。

## 在 Word 里配置 DeepSeek

1、打开 Word 软件,打开文件 -> 选项 -> 自定义功能区,勾选“开发者工具”,然后点确定。

2、打开文件 -> 选项->信任中心 ->信任中心设置,选择“启用所有宏”与“信任对 VBA......”,然后确定。

3、可以发现word选项卡中出现了一个“开发者工具”,点击开发者工具,然后点击左上角的Visual Basic,会弹出一个窗口。

选中“Normal”,点鼠标右键,弹出的菜单里选插入 ->模块。这时会弹出一个编辑器,然后输入以下脚本代码。


  1. Function CallDeepSeekAPI(api_key As String, inputText As String)
  2. Dim API As String
  3. Dim SendTxt As String
  4. Dim Http As Object
  5. Dim status_code As Integer
  6. Dim response As String
  7. API = "https://api.deepseek.com/chat/completions"
  8. SendTxt = "{""model"": ""deepseek-chat"", ""messages"": [{""role"":""system"", ""content"":""You are a Word assistant""}, {""role"":""user"", ""content"":""" & inputText & """}], ""stream"": false}"
  9. Set Http = CreateObject("MSXML2.XMLHTTP")
  10. With Http
  11. .Open "POST", API, False
  12. .setRequestHeader "Content-Type", "application/json"
  13. .setRequestHeader "Authorization", "Bearer " & api_key
  14. .send SendTxt
  15. status_code = .Status
  16. response = .responseText
  17. End With
  18. ' 弹出窗口显示 API 响应(调试用)
  19. ' MsgBox "API Response: " & response, vbInformation, "Debug Info"
  20. If status_code = 200 Then
  21. CallDeepSeekAPI = response
  22. Else
  23. CallDeepSeekAPI = "Error: " & status_code & " - " & response
  24. End If
  25. Set Http = Nothing
  26. End Function
  27. Sub DeepSeekV3()
  28. Dim api_key As String
  29. Dim inputText As String
  30. Dim response As String
  31. Dim regex As Object
  32. Dim matches As Object
  33. Dim originalSelection As Object
  34. api_key = "自己的API key"
  35. If api_key = "" Then
  36. MsgBox "Please enter the API key."
  37. Exit Sub
  38. ElseIf Selection.Type <> wdSelectionNormal Then
  39. MsgBox "Please select text."
  40. Exit Sub
  41. End If
  42. ' 保存原始选中的文本
  43. Set originalSelection = Selection.Range.Duplicate
  44. inputText = Replace(Replace(Replace(Replace(Replace(Selection.Text, "", "\"), vbCrLf, ""), vbCr, ""), vbLf, ""), Chr(34), """")
  45. response = CallDeepSeekAPI(api_key, inputText)
  46. If Left(response, 5) <> "Error" Then
  47. Set regex = CreateObject("VBScript.RegExp")
  48. With regex
  49. .Global = True
  50. .MultiLine = True
  51. .IgnoreCase = False
  52. .Pattern = """content"":""(.*?)"""
  53. End With
  54. Set matches = regex.Execute(response)
  55. If matches.Count > 0 Then
  56. response = matches(0).SubMatches(0)
  57. response = Replace(Replace(response, """", Chr(34)), """", Chr(34))
  58. ' 取消选中原始文本
  59. Selection.Collapse Direction:=wdCollapseEnd
  60. ' 将内容插入到选中文字的下一行
  61. Selection.TypeParagraph ' 插入新行
  62. Selection.TypeText Text:=response
  63. ' 将光标移回原来选中文本的末尾
  64. originalSelection.Select
  65. Else
  66. MsgBox "Failed to parse API response.", vbExclamation
  67. End If
  68. Else
  69. MsgBox response, vbCritical
  70. End If
  71. End Sub
复制代码

代码中的 Api_key = "自己的 API key",这里面把申请到的 API key 替换进去。

4、点击文件 -> 选项 -> 自定义功能区,选中开发工具点击右键,弹出的菜单里点击添加新组。然后把新组重命名为 DeepSeek。

自定义功能区->宏,找到 DeepSeekV3,添加到上面新建的DeepSeek 分组,然后把添加过去的宏改名为“开始对话”。如下图显示:

## 测试效果

完成上面的配置,在菜单里的开发工具里,会多一个“开始对话”的按钮,接下来就可以测试效果了。

打开文档,选中需要对话的内容,比如“请解释一下最近 DeepSeek 爆火的原因”,点开始对话。

哈哈,报错啦!提示余额不足。上面说过,DeepSeek 临时关了充值,赠送的 10 元也没有给我,等有钱了再用吧。

至于 WPS,大家可以自己动手去试试,应该大同小异。好了,今天的分享就到这里。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4008
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行