import openpyxl
from datetime import datetime, timedelta
# 定义固定日期 fixed_date = datetime(2023, 1, 1)
# 打开 Excel 文件
workbook = openpyxl.load_workbook('merged_file_2.xlsx')
# 选择工作表
worksheet = workbook.active
# 遍历列 G 中所有单元格,将日期转换成整数并减去固定日期,结果写入 H 列
for row in worksheet.iter_rows(min_row=2, min_col=7, max_col=7):
for cell in row:
cell_offset = cell.offset(column=1)
cell_date = datetime.date(cell.value)
cell_int = (cell_date - fixed_date.date()).days
cell_offset.value = cell_int# 保存 Excel 文件
workbook.save('merged_file_2.xlsx')
for row in worksheet.iter_rows(min_row=2, min_col=7, max_col=7):
for cell in row:
cell_offset = cell.offset(column=1)
if cell.value is None:
continue
cell_date = datetime.date(cell.value)
cell_int = (cell_date - fixed_date.date()).days
cell_offset.value = cell_int
import openpyxl
from datetime import datetime, timedelta
# 定义固定日期
fixed_date = datetime(2023, 4, 2)
# 打开 Excel 文件
workbook = openpyxl.load_workbook('merged_file_2.xlsx')
# 选择工作表
worksheet = workbook.active
# 遍历列 G 中所有单元格,将日期转换成整数并减去固定日期,结果写入 H 列
for row in worksheet.iter_rows(min_row=2, min_col=7, max_col=7):
for cell in row:
cell_offset = cell.offset(column=1)
if cell.value is None:
continue
cell_date = cell.value.date()
cell_int = (cell_date - fixed_date.date()).days
cell_offset.value = cell_int
# 保存 Excel 文件
workbook.save('merged_file_2.xlsx')
小黑屋|Archiver|手机版|su.sseuu.com ( 粤ICP备12005776号 )
GMT+8, 2024-10-15 16:24 , Processed in 0.099687 second(s), 16 queries .
Powered by Discuz! X3.4 Licensed
© 2001-2013 Comsenz Inc.