网站首页 > 技术文章 正文
2025 年春节,Deepseek 那可真是大火特火,出了圈啦!WPS 呢,作为一种文本处理的工具,它自身是带有 AI 功能的,不过要是想用这个 AI 功能,就得开通会员。Deepseek 呢,是个特别厉害的搜索和信息提取的工具,在好多场景里都能起到重要作用。这篇文章会给您讲讲,在不用任何额外插件的情况下,通过一些简简单单的步骤,把 Deepseek 的功能弄到 WPS Office 里,这样就能让平常办公的效率更高啦。
使用效果:
选择文字,点击功能菜单中的生成,即可实现和Deepseek的对话,并且让回复的文字直接进入WPS中,不需要切换应用。如下图:
实现步骤:
- 准备API key
获取地址:
- deepseek-r1模型API Key获取地址:https://www.deepseek.com/
- qwen2.5模型API Key获取地址:
https://bailian.console.aliyun.com/
(附由于当前的deepseek很火,API可能无法访问,可以使用千问的作平替)
- 配置WPS
(1)配置信任中心,启用宏。详细步骤:找到WPS的“选项”对话框,找到“信息中心”,勾选“启用宏.....”
(2)增加模块
详细步骤:找到“开发工具”,选择“VB编辑器”,打开新的窗口
选择“插入”菜单,找到“模块”
打开模块窗口,输入代码(附后)
特别提醒:将代码复制到编辑区中(**注意替换你自己的API key**)
将以上操作完成后关闭VB编辑窗口。
(3)设置功能菜单
进入“选项”对话框,找到“自定义功能区”,在右侧主选项卡的“开始”,选择下面的按钮“新建组”,这里名称可以自定义。我输入的是“AI智能”
选择刚新添加的“新建组”,找到左侧的命令区,选择“宏”找到刚才添加的宏模块,然后添加即可。(注:这里可以根据需要修改名称)
完成以上的操作,进入WPS文字之后,在“开始”选项卡,最后就会出现一个刚才添加的模块。
- 使用方法:
选中需要处理的文字,点击刚才自定义的按钮,等待大模型响应。(此时响应的过程是有点慢,请耐心等待)
(由于DeepSeek的KEY暂时不能使用,这里使用了通义千问)
附件:
DeepSeek V3代码:
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
通义千问Max2.5 代码:
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
猜你喜欢
- 2025-05-26 学习HackerOne上Flink、Grafana、jolokia攻击手法
- 2025-05-26 使用ANTLR开发自己的DSL语言(一)
- 2025-05-26 Spring技巧:深入研究Java 14和SpringBoot
- 2025-05-26 Nginx常见问题
- 2025-05-26 MyBatis系列-2-日志配置
- 2025-05-26 免费且好用的乐谱制作软件
- 2025-05-26 进阶版Python正则表达式大全,看到就赚到了
- 2025-05-26 在word中通过VBA调用百度翻译API在线翻译
- 2025-05-26 Java文件读取终极指南:4种方式对比与性能优化实战
- 2025-05-26 Groovy和Java
- 最近发表
-
- 使用这个新的 ECMAScript 运算符告别 Try/Catch!
- 抛弃 try-catch,错误处理的新方案
- 深圳尚学堂Java培训:总结java编程常用的快捷键(二)
- Try-catch speeding up my code?(speeding up)
- 能代替try catch处理异常的优雅方式
- Linux系统stress压力测试工具(linux自带的压力测试)
- ESL-通过事件控制FreeSWITCH(es事务控制)
- 谈JVM xmx, xms等内存相关参数合理性设置
- 嵌入式工程师竟然看不懂这些专业语句,那真别怪人说你菜
- 不会前端也能写官网?没问题,Devbox+Cursor 带你起飞
- 标签列表
-
- cmd/c (64)
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- sqlset (64)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)