You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python调用Confluence REST API更新表格

Confluence表格数据追加方案

现有读取代码

你目前已实现通过Python读取Confluence页面表格的代码如下:

url = 'https://ConflunecePageLocation/rest/api/content/ContentIDNumber?expand=body.view'
response = requests.get(url,verify=False,auth=headeroauth)
print(response.status_code)
data=response.text
df=pd.read_html(data,flavor='lxml')
df3=pd.DataFrame(df[0])

需求

将Excel文件中的5行数据追加到该Confluence页面的原表格末尾,避免复杂的手动HTML格式处理。

可行方案

核心思路是:读取页面可编辑的原始存储内容,提取原表格数据并与新数据合并,用pandas自动生成简洁HTML表格,替换原页面表格后更新页面。具体实现代码如下:

import requests
import pandas as pd
from bs4 import BeautifulSoup

# 获取页面存储格式内容及当前版本号
url = 'https://ConflunecePageLocation/rest/api/content/ContentIDNumber?expand=body.storage'
response = requests.get(url, verify=False, auth=headeroauth)
page_info = response.json()
storage_content = page_info['body']['storage']['value']
current_version = page_info['version']['number']

# 解析原表格数据
soup = BeautifulSoup(storage_content, 'lxml')
original_table = soup.find('table')
df_original = pd.read_html(str(original_table), flavor='lxml')[0]

# 读取Excel中的新数据
df_new = pd.read_excel('待追加数据.xlsx')

# 合并原数据与新数据
df_combined = pd.concat([df_original, df_new], ignore_index=True)

# 生成不带多余样式的HTML表格
new_table_html = df_combined.to_html(index=False, border=1)

# 替换页面中的原表格
updated_content = storage_content.replace(str(original_table), new_table_html)

# 调用API更新页面
update_url = 'https://ConflunecePageLocation/rest/api/content/ContentIDNumber'
update_payload = {
    "version": {"number": current_version + 1},
    "body": {
        "storage": {
            "value": updated_content,
            "representation": "storage"
        }
    }
}

update_response = requests.put(update_url, json=update_payload, verify=False, auth=headeroauth)
print(f"更新请求状态码: {update_response.status_code}")

方案说明

  1. 使用body.storage而非body.view获取页面可编辑的原始内容,确保能正确更新页面。
  2. 借助BeautifulSoup定位原表格,避免手动查找HTML结构。
  3. 利用pandas的to_html自动生成标准表格HTML,无需手动编写复杂标签。
  4. 仅替换页面中的表格部分,保留页面其他内容不变。

内容的提问来源于stack exchange,提问作者Raj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 00:02:18