随着DeepSeek等人工智能技术的飞速发展,把AI能力直接融入日常办公软件已经成为不可逆转的大趋势。办公软件如WPS、PDFgear、Notion等都融合有AI功能。
之前写了一篇把DeepSeek接入Word的文章,现在把WPS的也补上。
https://pythonfun.blog.csdn.net/article/details/145837432
很多同学希望在写作、翻译、润色文稿时,能够随时调用大模型来帮忙。虽然有OfficeAI助手等插件辅助,但是为了安全起见,我们采用WPS VBA宏代码,把 DeepSeek V3模型接入到 WPS 中,同时给它设定一个快捷方式,方便随时调用。
一、准备工作
要在WPS中使用 DeepSeek,我们需要做三件事:
1.获取API密钥 :这是访问 DeepSeek 服务的通行证,类似一把钥匙。可以通过注册silicon flow(硅基流动 http://cloud.siliconflow.cn)来获得,其实就是一串以"sk-"开头的字符串。
2. 打开宏功能:WPS 默认关闭宏,需要到"选项 → 信任中心"里启用宏支持。
3. 进入VBA 编辑器:按下快捷键【Alt + F11】,或者通过【开发工具】---【VB编辑器】就可以打开宏的编辑界面。
这三步完成后,环境就准备好了。
二、导入宏代码
进入VBA 编辑器后,点击【视图】,找到Normal模板下的模块,右击它新建一个模块,并将其属性改为:DeepSeekV3。
然后,把下面这段VBA代码完整复制进去,把代码中API_KEY处替换成你自己的,最后保存并关闭编辑器。
vbnet
Function CallDeepSeekAPI(api_key As String, inputText As String) As String
Dim API As String
Dim SendExt As String
Dim Http As Object
Dim status_code As Integer
Dim response As String
API = "http://api.siliconflow.cn/v1/chat/completions"
SendExt = "{""model"": ""deepseek-ai/DeepSeek-V3"", ""messages"": [{""role"":""system"",""content"":""You are a helpful assistant.""},{""role"":""user"",""content"":""" & inputText & """}]}"
On Error Resume Next
Set Http = CreateObject("MSXML2.XMLHTTP.6.0")
If Err.Number <> 0 Then
CallDeepSeekAPI = "Error: Failed to create HTTP object - " & Err.Description
Exit Function
End If
With Http
.Open "POST", API, False
.setRequestHeader "Content-Type", "application/json; charset=utf-8"
.setRequestHeader "Authorization", "Bearer " & api_key
.Send SendExt
If Err.Number <> 0 Then
CallDeepSeekAPI = "Error: API request failed - " & Err.Description
Exit Function
End If
status_code = .Status
response = .responseText
End With
On Error GoTo 0
If status_code = 200 Then
CallDeepSeekAPI = response
Else
CallDeepSeekAPI = "HTTP " & status_code & " : " & response
End If
Set Http = Nothing
End Function
Sub DeepSeekV3_Simplest()
Dim api_key As String
Dim inputText As String
Dim response As String
Dim content As String
Dim originalSelection As Range
Dim startPos As Long
Dim endPos As Long
' 设置 API 密钥
api_key = "<你硅基流动的API_KEY>"
' 检查 API 密钥是否为空
If api_key = "" Then
MsgBox "请填写API密钥", vbCritical
Exit Sub
End If
' 检查是否选择了文本
If Selection.Type <> wdSelectionNormal Then
MsgBox "请先选择需要处理的文本", vbExclamation
Exit Sub
End If
' 保存当前选中的文本范围
Set originalSelection = Selection.Range
inputText = Trim(Replace(Selection.Text, vbCr, ""))
' 调用 DeepSeek API
response = CallDeepSeekAPI(api_key, inputText)
' 检查 API 调用是否出错
If Left(response, 5) = "Error" Or Left(response, 4) = "HTTP" Then
MsgBox response, vbCritical
Exit Sub
End If
' 简单提取content内容(不使用JSON解析)
startPos = InStr(response, """content"":""") + 11
endPos = InStr(startPos, response, """},""")
If startPos > 11 And endPos > startPos Then
content = Mid(response, startPos, endPos - startPos)
' 清理格式
content = Replace(content, "\n", vbCrLf)
content = Replace(content, "\""", """")
content = Replace(content, "**", "")
content = Replace(content, "###", "")
' 插入到文档
originalSelection.Collapse Direction:=wdCollapseEnd
originalSelection.InsertAfter vbCrLf & vbCrLf & "AI回答:" & vbCrLf & content
Else
MsgBox "无法解析API响应", vbExclamation
End If
End Sub
三、添加到快捷工具栏
如果每次都要进宏菜单运行,会比较麻烦。为了方便使用,我们可以把它放到WPS 的"快捷工具栏"里。方法如下:
-
打开 WPS ,依次点击【选项】--- 【自定义功能区】 --- 【快速访问工具栏】。
-
在左侧的类别中选择宏,找到Normal开头、名称为DeepSeekV3的选项。
-
点击 【添加】,把它放到右边的工具栏列表。
-
确认后保存,WPS 界面左上方多了一个快捷按钮。
-
还可以在【自定义工具栏】处,找到对应的宏,给它分配一个快捷键,如Ctrl + Shift + D,这样用起来更快。
以后只需要选中文字,点一下快捷按钮,或者按下快捷键,就得获取DeepSeek的回答。
四、注意事项
-
API 密钥不能为空:第一次使用前,需要把代码中的密钥位置替换为你自己的。
-
必须先选中内容:宏只处理当前选中的文字,如果没有选择,宏会弹窗提醒。
-
网络需要保持畅通:宏是通过互联网与 DeepSeek 交互,没有网络就无法返回结果的。
五、总结
我们通过编程宏,开启宏,导入代码,设置快捷方式,添加快捷按钮,选中文本运行,实现了在WPS当中调用DeepSeek V3的api,不需要切换软件,不需要复制粘贴,就能直接请DeepSeek在文档里辅助你完成各项任务。
由于我们把宏代码放到了Normal模板里,这样每次打开WPS都可以调用它用于日常撰写文章、整理报告、翻译材料时,大幅提升办公效率。
如果感兴趣就赶快实践一下吧!期待你的留言反馈。
关注我,学习更多Python和大模型应用技能。