我是python和openpyxl的新手。我开始学习以便使我的日常工作在工作场所中更轻松,更快捷。
任务: 有一个包含很多行的excel文件,看起来像这样 excel file
我想基于此excel文件创建每日报告。在我的示例中,今天是2019/05/08。
预期结果: 仅显示日期与“今天”日期匹配的信息。 预期结构:
我的解决方案 在我的解决方案中,我创建了一个行列表,在其中只能找到Today值。之后,我仅读取该行并创建字典。但是结果什么都没有。对于如何使用多个键,我也很麻烦。因为列表中有多个问题编号。
from datetime import datetime
import openpyxl
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
from openpyxl.utils import column_index_from_string
#Open excel file
excel_path = "\\REE.xlsx"
wb = openpyxl.load_workbook(excel_path, data_only=True)
ws_1 = wb.worksheets[1]
#The Today date. need some format due to excel date handling
today = datetime.today()
today = today.replace(hour=00, minute=00, second=00, microsecond=00)
#Crate a list of the lines where only Today values are present
issue_line_list = []
for cell in ws_1["B"]:
if cell.value == today:
issue_line = cell.row
issue_line_list.append(issue_line)
#Creare a txt file for output
file = open("daily_report.txt", "w")
#The dict what I want to use
dict = []
issue_numbers_list = []
issue = []
#Create a dict for the issues
for line in issue_line_list:
issue_number_value = ws_1.cell(row = line, column = 3).value
issue_numbers_list.append(issue_number_value)
#Create a dict for other information
for line in issue_line_list:
issue_number_value = ws_1.cell(row = line, column = 3).value
by_value = ws_1.cell(row = line, column = 2 ).value
group_value = ws_1.cell(row = line, column = 4).value
events_value = ws_1.cell(row = line, column = 5).value
deadline_value = ws_1.cell(row = line, column = 6).value
try:
deadline_value = deadline_value.strftime('%Y.%m.%d')
except:
deadline_value = ""
issue.append(issue_number_value)
issue.append(by_value)
issue.append(group_value)
issue.append(events_value)
issue.append(deadline_value)
issue.append(deadline_value)
#Append the two dict
dict.append(issue_numbers_list)
dict.append(issue)
#Save it to the txt file.
file.write(dict)
file.close()
问题 -如何解决多个相同的关键问题? -如何创建嵌套组? -为了获得预期的结果,应该在我的代码中添加或删除哪些内容?
备注 Openpyxl不仅是选项。如果您有更好/更轻松/更快的方式,我会为每个想法开放。
预先感谢您的支持!
答案 0 :(得分:0)
您可以尝试以下方法吗?
import pandas as pd
cols = ['date', 'by', 'issue_number', 'group', 'events', 'deadline']
req_cols = ['events', 'deadline']
data = [
['2019-05-07', 'john', '113140', '@issue_closed', 'something different', ''],
['2019-05-08', 'david', '113140', '@task', 'something different', ''],
['2019-05-08', 'victor', '114761', '@task_result', 'something different', ''],
['2019-05-08', 'john', '114761', '@task', 'something different', '2019-05-10'],
['2019-05-08', 'david', '114761', '@task',
'something different', '2019-05-08'],
['2019-05-08', 'victor', '113140', '@task_result', 'something different', ''],
['2019-05-07', 'john', '113140', '@issue_created',
'something different', '2019-05-09'],
['2019-05-07', 'david', '113140', '@location', 'something different', ''],
['2019-05-07', 'victor', '113140', '@issue_closed', 'something different', 'done'],
['2019-05-07', 'john', '113140', '@task_result', 'something different', ''],
['2019-05-07', 'david', '113140', '@task',
'something different', '2019-05-10'],
]
df = pd.DataFrame(data, columns=cols)
df1 = df.groupby(['issue_number', 'group']).describe()[req_cols].droplevel(0, axis=1)['top']
df1.columns = req_cols
print(df1)
输出:
events deadline
issue_number group
113140 @issue_closed something different done
@issue_created something different 2019-05-09
@location something different
@task something different 2019-05-10
@task_result something different
114761 @task something different 2019-05-08
@task_result something different
要打开excel
文件,您可以执行以下操作:
df = pd.read_excel(excel_path, sheet_name=my_sheet)
req_cols = ['EVENTS', 'DEADLINE']
df1 = df.groupby(['ISSUE NUMBER', 'GROUP']).describe()[req_cols].droplevel(0, axis=1)['top']
df1.columns = req_cols
print(df1)
答案 1 :(得分:0)
任务几乎解决了,但是我遇到了一个新问题。
代码:
excel_path = "\\REE.xlsx"
my_sheet = 'Events'
cols = ['DATE', 'BY', 'ISSUE NUMBER', 'GROUP', 'EVENTS', 'DEADLINE']
req_cols = ['EVENTS', 'DEADLINE']
df = pd.read_excel(excel_path, sheet_name = my_sheet, columns=cols)
today = datetime.today().strftime('%Y-%m-%d')
today_filter = (df[(df['DATE'] == today)])
df = pd.DataFrame(today_filter, columns=cols)
df1 = df.groupby(['ISSUE NUMBER', 'GROUP']).describe()[req_cols].droplevel(0, axis=1['top']
df1.columns = req_cols
print(df1)
在“ BY”列上有相同的值。例如。 '@任务'。但是脚本只打印一次。
必填结果:
114761
@task Jane another words 2019-05-10
@task result John something
@task John something else 2019-05-08
...
...
...
...
我的代码结果:
114761
@task Jane another words 2019-05-10
@task result John something
...
...
...
John @task还有其他内容2019-05-08请勿将其打印出来。为什么?
其他选项也有一些结果。如果“ BY”列中还有其他值,则脚本仅打印第一个,然后跳过其余的。