Python+onlyoffice 实现在线word编辑

onlyoffice部署

yml 复制代码
version: "3"
services:
	onlyoffice:
	    image: onlyoffice/documentserver:7.5.1
	    container_name: onlyoffice
	    restart: always
	    environment:
	      - JWT_ENABLED=false
	        #- USE_UNAUTHORIZED_STORAGE=true
	        #- ONLYOFFICE_HTTPS_HSTS_ENABLED=false
	
	    ports:
	      - "8080:80"
	        # - "8443:443"
	
	    volumes:
	      - ./data/onlyoffice/log:/var/log/onlyoffice
	      - ./data/onlyoffice/data:/var/www/onlyoffice/Data
	      - ./data/onlyoffice/lib:/var/lib/onlyoffice
	      - ./data/onlyoffice/postgresql:/var/lib/postgresql
	      # - ./default.json:/etc/onlyoffice/documentserver/default.json

修改default.json

docker cp onlyoffice:/etc/onlyoffice/documentserver/default.json ./

修改以下值为:

json 复制代码
...
"request-filtering-agent" : {
				"allowPrivateIPAddress": true,
				"allowMetaIPAddress": true
			},
...

在docker-compose中添加挂载:

- ./default.json:/etc/onlyoffice/documentserver/default.json

前端代码

保存为 test.html

html 复制代码
<!DOCTYPE html>
<html>

<head>

    <script type='text/javascript' src='http://IP:PORT/web-apps/apps/api/documents/api.js'></script>
    <style>
        html {
            height: 100%;
            width: 100%;
            padding: 0;
            margin: 0;
        }

        body {
            height: 100%;
            width: 100%;
            padding: 0;
            margin: 0;
        }
    </style>


</head>


<body>
    <div id="placeholder"></div>
    <script language="javascript" type="text/javascript">
        var docEditor = new DocsAPI.DocEditor("placeholder", {
            "document": {
                // 文件类型
                "fileType": "docx",
                // 每次需要重新生成随机字符串
                "key": "adfgadfgadfgad",
                // 标题
                "title": "test.docx",
                // 文档地址,GET
                "url": "http://IP:PORT/api/file?name=test.docx"
            },
            "documentType": "word",
            "editorConfig": {
                // 保存文档的回调地址, POST
                "callbackUrl": "http://IP:PORT/api/file?name=test.docx",
                // 查看模式
                // "mode": "view",
                // 编辑模式
                "mode": "edit",
                // 中文 
                "lang": "zh-CN",
                "customization": {
                    // 插件
                    "plugins":false,
                    // 关于
                    "about": false,
                    // 自动保存
                    "autosave":true,
                    // 聊天
                    "chat":false,
                    // 批注
                    "comments":false,
                    // 帮助
                    "help":false,
                }
            }
        });
    </script>
</body>

</html>

python回调接口

python 复制代码
import requests
from flask import Flask, request

app = Flask(__name__)

@app.get("/api/file")
def get_file():
	file_path = "/data/test.docx"
    return make_response(send_file(file_path, as_attachment=True))


@app.post("/api/file")
def save_file():
	file_path = "/data/test.docx"
    callback_obj = request.get_json()
    print(callback_obj)
    status = callback_obj.get("status", -1)
	
    if status == 2 or status == 3 or status == 6:
        try:
            r = requests.get(callback_obj["url"])
            with open(file_path, "wb") as f:
                f.write(r.content)
        except Exception as e:
            print("保存错误:", e)
            return {"error": 1}

    return {"error": 0}
相关推荐
ONLYOFFICE2 天前
入门指南:远程运行 ONLYOFFICE 协作空间 MCP 服务器
运维·服务器·github·onlyoffice
ONLYOFFICE6 天前
如何在 ONLYOFFICE 桌面编辑器中连接本地 AI
人工智能·onlyoffice
hhzz8 天前
Docker 搭建 NextCloud + OnlyOffice 完整教程(Linux Centos7系统)
linux·docker·容器·onlyoffice·nextcloud
CodeCraft Studio25 天前
用“录制宏”轻松实现文档自动化:ONLYOFFICE 宏功能实践解析
运维·自动化·onlyoffice·录制宏·创建宏·文档自动化·文档协同
ONLYOFFICE1 个月前
如何在Windows ARM64 上安装ONLYOFFICE桌面编辑器:完整指南
编辑器·arm·onlyoffice
ONLYOFFICE1 个月前
如何将 ONLYOFFICE 协作空间 MCP 服务器连接到桌面编辑器
onlyoffice·mcp 服务器
java_logo1 个月前
Onlyoffice Documentserver Docker 容器化部署指南
运维·人工智能·docker·容器·onlyoffice·milvus·documentserver
ONLYOFFICE1 个月前
如何在ONLYOFFICE协作空间中使用AI智能体?配置应用全攻略
人工智能·ai·编辑器·onlyoffice
夏微凉秋微暖1 个月前
onlyoffice在线预览和编辑(编辑后续研究)
onlyoffice
ONLYOFFICE1 个月前
ONLYOFFICE 文档与桌面编辑器 9.2 版本更新说明
人工智能·编辑器·onlyoffice