使用Python JIRA API导出Jira评论至Excel时遇AttributeError问题求解
问题:导出Jira评论至Excel时触发AttributeError错误
尝试用Python导出Jira项目所有问题评论到Excel,编写了以下代码:
Comments_list.append(issue.fields.comment.comments) for comment in Comments_list: ws.cell(row=comments_row, column=comments_column).value = comment.body comments_row += 1
运行时出现错误:
AttributeError: 'PropertyHolder' object has no attribute 'comment'
解决方法
1. 确保获取Issue时拉取了评论字段
这个错误核心是issue.fields里没有comment属性——默认调用Jira API获取Issue时,不会返回评论这类扩展字段。你需要在获取Issue时显式指定要拉取的字段:
# 获取单个Issue时指定fields参数 issue = jira.issue(issue_key, fields='comment') # 或者用expand参数获取完整的评论内容 issue = jira.issue(issue_key, expand='comments')
如果是批量搜索Issue,同样要在搜索时指定字段:
issues = jira.search_issues(jql_query, fields='comment')
2. 修正评论列表的遍历逻辑
原代码把整个评论列表append到Comments_list,导致Comments_list是嵌套列表(比如[[comment1, comment2]]),就算前面的字段问题解决,后续遍历也会触发'list' object has no attribute 'body'错误。正确写法有两种:
写法一:直接遍历评论写入Excel
# 先检查是否存在评论字段及评论内容 if hasattr(issue.fields, 'comment') and issue.fields.comment.comments: for comment in issue.fields.comment.comments: ws.cell(row=comments_row, column=comments_column).value = comment.body comments_row += 1
写法二:先收集所有评论再遍历
Comments_list = [] # 检查字段存在性后,用extend添加单个评论,而非append整个列表 if hasattr(issue.fields, 'comment'): Comments_list.extend(issue.fields.comment.comments) for comment in Comments_list: ws.cell(row=comments_row, column=comments_column).value = comment.body comments_row += 1
3. 增加字段存在性检查
不是所有Jira问题都有评论,所以每次访问issue.fields.comment前,先用hasattr()检查字段是否存在,避免触发属性不存在的错误。
内容的提问来源于stack exchange,提问作者OliAK
相关产品推荐
相关产品推荐

