技术问询:如何用Python按项目及经办人获取Jira数据并导出至Excel
Great question! Let's build on your existing code to achieve this—here's a step-by-step solution that covers querying Jira issues by project and assignee, extracting relevant details, and exporting to Excel.
Step 1: Install Required Libraries
You already have the jira library, but we'll need pandas and openpyxl for seamless Excel exports. Install them if you haven't:
pip install jira pandas openpyxl
Step 2: Initialize Jira Client & Query Issues with JQL
Instead of fetching a single issue, we'll use JQL (Jira Query Language) to filter issues by your target project and assignee. This lets you pull all matching issues at once:
from jira import JIRA import pandas as pd # Update these with your actual Jira credentials and server URL options = {'server': 'https://your-jira-instance.com', 'verify': False} jira = JIRA(options, basic_auth=('your-username', 'your-password')) # Define your filters (customize these values) target_project = 'DWO' target_assignee = 'john.doe' # Use the assignee's Jira username/email # JQL query to fetch matching issues (adjust filters like status if needed) jql_query = f'project = {target_project} AND assignee = "{target_assignee}" AND status != Closed' # maxResults=False fetches all matching issues (use a number if you want a limit) issues = jira.search_issues(jql_query, maxResults=False)
Step 3: Extract Relevant Issue Details
Loop through each issue to collect the fields you care about. Customize this list based on your specific needs:
issue_data = [] for issue in issues: # Extract core fields (add/remove fields as required) issue_details = { 'Issue Key': issue.key, 'Summary': issue.fields.summary, 'Description': issue.fields.description or 'No description', 'Status': issue.fields.status.name, 'Assignee': issue.fields.assignee.displayName if issue.fields.assignee else 'Unassigned', 'Created Date': issue.fields.created, 'Updated Date': issue.fields.updated, 'Priority': issue.fields.priority.name if issue.fields.priority else 'No priority', 'Labels': ', '.join(issue.fields.labels) if issue.fields.labels else 'None' } issue_data.append(issue_details)
Step 4: Export to Excel
Using pandas, converting our collected data to an Excel file is straightforward:
# Convert to a pandas DataFrame for easy manipulation df = pd.DataFrame(issue_data) # Export to Excel (customize the filename as you like) output_filename = f'{target_project}_{target_assignee}_issues.xlsx' df.to_excel(output_filename, index=False, engine='openpyxl') print(f"Success! Exported {len(issues)} issues to {output_filename}")
Key Customizations & Tips
- JQL Flexibility: Tweak the JQL query to add more filters (e.g.,
AND created >= "-30d"for issues from the last 30 days, orAND issuetype = Bugfor only bug reports). - Custom Fields: To extract custom Jira fields, use
issue.fields.customfield_XXXXwhereXXXXis the field's ID. You can find this ID by inspecting the issue page's HTML or using Jira's REST API. - Error Handling: Add
try-exceptblocks if you're dealing with large datasets or optional fields (e.g., some issues might not have an assignee or priority). - Pagination: For thousands of issues, replace
maxResults=Falsewith pagination using thestartAtparameter to avoid timeouts.
内容的提问来源于stack exchange,提问作者Himanshu Pant




