把DeepSeek接入WPS自动化办公竟然这么丝滑
获取API Key
登录DeepSeek官网,申请即可
https://www.deepseek.com/
创建一个API Key
这里获取到的Key需要妥善保管,一旦忘记是无法找回的
OfficeAI插件方式
默认WPS是没有OfficeAI插件的,需要自己安装
下载地址:链接: https://pan.baidu.com/s/10EiSTxcFf1tRDOykc9c7Qw?pwd=9xnx 提取码: 9xnx
直接安装即可
这里下载附加文件可能有点慢,大家耐心等一会就好,安装完成后会出现OfficeAI菜单栏
点击设置,会提示需要登录,先扫码登录
再点击设置,打开本地模型开关,默认该插件是带了一些模型的,比如豆包、文心一言等等,这里我们输入DeepSeek API Key
因为官网的API有时候不稳定,我们这里也可以选择硅基流动
注意这里模型要选择正确哦
PS:硅基流动目前是注册就赠送2000万token,大家可以通过我的链接注册哈,我已经拥有2000千多额度啦 https://cloud.siliconflow.cn/i/D1TsLVHr
此时会要求安装本地服务,点击安装即可
接下来我们就可以和DeepSeek助手对话啦
VBA插件方式
下载安装VBA插件
下载地址:链接: https://pan.baidu.com/s/1xliJ6RLikAeLPzWdrm_I3A?pwd=7jsk 提取码: 7jsk
下载完成后,直接双击安装即可
配置WPS
新建创建一个WPS文本
调出工具菜单栏
点击文件-》选项-》自定义功能区,查看工具菜单栏是否勾选
设置宏安全性
点击文件-》选项-》信任中心-》宏安全性,选择低
同时勾选如下两项
点击工具栏的开发工具选项
点击VB编辑器
右击Normal
插入-》模块
复制DeepSeek R1代码
Function CallDeepSeekAPI(api_key As String, inputText As String) As StringDim API As StringDim SendTxt As StringDim Http As ObjectDim status_code As IntegerDim response As StringAPI = "https://api.deepseek.com/chat/completions"SendTxt = "{""model"": ""deepseek-reasoner"", ""messages"": [{""role"":""system"", ""content"":""你是一个乐于助人的AI助手,请根据用户的问题给出详细的解答。""}, {""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 = .responseTextEnd With' 弹出窗口显示 API 响应(调试用)' MsgBox "API Response: " & response, vbInformation, "Debug Info"If status_code = 200 Then CallDeepSeekAPI = responseElse CallDeepSeekAPI = "Error: " & status_code & " - " & responseEnd IfSet Http = NothingEnd FunctionSub DeepSeekR1()Dim api_key As StringDim inputText As StringDim response As StringDim regex As ObjectDim reasoningRegex As ObjectDim contentRegex As ObjectDim matches As ObjectDim reasoningMatches As ObjectDim originalSelection As ObjectDim reasoningContent As StringDim finalContent As Stringapi_key = "替换为你的api key"If api_key = "" Then MsgBox "Please enter the API key." Exit SubElseIf Selection.Type <> wdSelectionNormal Then MsgBox "Please select text." Exit SubEnd If' 保存原始选中的文本Set originalSelection = Selection.Range.DuplicateinputText = 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, "nn", 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, "nn", 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 IfElse MsgBox response, vbCriticalEnd IfEnd Sub
注意这里替换成你的API Key
关闭VB编辑器,回到WPS页面
在工具菜单栏下创建一个自己的AI助手
选中工具,点击下方的“新建组”,然后重名为自定义的名称
接下来绑定编辑好的VB宏
左侧下拉框选择“宏”,再选择DeepSeek R1,添加到自定的助手当中
确定即可
这个时候我们工具菜单栏下面就会出现VB宏了
测试使用
这个时候我们输入一段文字,然后点击这个AI助手,就可以和DeepSeek交互了
这样就方便很多了,应用场景有很多,比如让大模型检测错别字,让大模型扩写,翻译等等,总之可以极大的提高我们的文字输出效率
添加带有VB宏的模版
为了达到每次进入WPS都可以直接使用AI助手,我们需要把带有VB宏的文件保存为模板,并且放到WPS的模板目录当中
先保存模板
文件-》另存为,选择文件类型为“带有宏的模板文件”
把该文件放到如下目录:C:Users用户名AppDataRoamingkingsoftwpsstartup
这个时候我们再新建WPS文档,就是带有VB宏的啦
版权声明:
作者:小火箭shadowrocket
链接:https://www.shadowrocket888.top/49.html
来源:小火箭官网
文章版权归作者所有,未经允许请勿转载。


共有 0 条评论