如何删除ADO Wiki页面content字段中的指定表格记录?
删除ADO Wiki页面Markdown表格中的指定行
需要删除ADO Wiki页面content字段内Markdown表格的指定记录行,保留表头。现有Python代码已能获取页面JSON数据,目标移除行明确为:| my-12345-vm-0 | #12345 | hi.work@gmail.com | 29-01-2024 |。
实现步骤
- 解析获取到的
content内容,按行拆分 - 过滤目标行,保留表头、表格分隔线及其他有效记录
- 将过滤后的内容重新拼接为合法的Markdown表格
- 调用ADO Wiki更新接口提交修改
修改后的Python代码
import requests import base64 import json def wiki_update(vm_list): # 获取PAT并构建认证头 pat = get_or_set_secret(kv_url, pat_secret_name, " ") authorization = base64.b64encode(f":{pat}".encode('ascii')).decode('ascii') headers = {"Authorization": f"Basic {authorization}", "Content-Type": "application/json"} # 获取页面原始数据 get_response = requests.get(wiki_url, verify=True, headers=headers) page_data = json.loads(get_response.text) # 处理表格内容,删除指定行 content_lines = page_data['content'].split('\n') # 精确匹配要删除的目标行 target_line = '| my-12345-vm-0 | #12345 | hi.work@gmail.com | 29-01-2024 |' # 过滤逻辑:保留非空行且排除目标行 new_content_lines = [line for line in content_lines if line.strip() != '' and line != target_line] new_content = '\n'.join(new_content_lines) # 构造更新请求体 update_payload = {"content": new_content} # 发送更新请求 update_response = requests.put(wiki_url, json=update_payload, headers=headers, verify=True) # 输出更新结果 if update_response.status_code == 200: print("页面更新成功") else: print(f"更新失败,状态码:{update_response.status_code},响应:{update_response.text}")
关键注意事项
- 确保
wiki_url为正确的ADO Wiki页面API地址,格式示例:https://dev.azure.com/{组织名}/{项目名}/_apis/wiki/wikis/{WikiID}/pages/{页面路径}?api-version=7.1-preview.1 - 目标行必须与原
content中的内容完全匹配(包括空格、竖线位置) - 用于认证的PAT需拥有Wiki页面的读写权限(权限范围勾选
Wiki Read & Write)
内容的提问来源于stack exchange,提问作者JonH
相关产品推荐
相关产品推荐

