我正在尝试从现有的excel文件中获取一组数据,并尝试仅在与日期匹配的情况下写入新文件。我正在尝试执行此操作,但不确定如何执行此操作。
我想要做的是获取具有雇佣日期的数据并将其写入新的Excel文件中。预期输出为:
代码:
def recreate_xls_file(src_file_path):
book_xls = xlrd.open_workbook(src_file_path)
book_xlsx = Workbook()
sheet_names = book_xls.sheet_names()
for sheet_index in range(0,len(sheet_names)):
sheet_xls = book_xls.sheet_by_name(sheet_names[sheet_index])
if sheet_index == 0:
sheet_xlsx = book_xlsx.active
sheet_xlsx.title = sheet_names[sheet_index]
else:
sheet_xlsx = book_xlsx.create_sheet(title=sheet_names[sheet_index])
headerrow = book_xls.sheet_by_index(0).row_values(0)
for col in range(0, sheet_xls.ncols):
sheet_xlsx.cell(row = 1 , column = col+1).value = headerrow[col]
for row in range(1, sheet_xls.nrows):
for col in range(1, sheet_xls.ncols):
if col == 3: #col 16 is the date
converted_datetime = datetime.datetime(*xlrd.xldate_as_tuple(sheet_xls.cell_value(row, col), book_xls.datemode))
converted_datetime2 = converted_datetime.strftime('%#m/%#d/%Y %#I:%M:%S %p')
cd_split= converted_datetime2.split()
date_str = cd_split[0]
date_now = datetime.datetime.now().date().strftime('%#m/%#d/%Y')
if date_str == date_now:
sheet_xlsx.cell(row = row+1 , column = 3).value = converted_datetime
sheet_xlsx.cell(row = row+1 , column = col+1).value = sheet_xls.cell_value(row, col)
book_xlsx.save(xlsx_file)
对此表示任何帮助,我们将不胜感激。谢谢
答案 0 :(得分:2)
我建议使用pandas
。它可以用几行代码(如果需要,甚至一行)解决问题:
import datetime
import pandas as pd
# read excel table
data = pd.read_excel('1.xlsx', parse_dates=['employment_date'])
# filter data
new_data = data.loc[data['employment_date'] == datetime.date.today(), :]
# save filtered data as excel table
new_data.to_excel('2.xlsx', index=False)