我是python的新手,目前正在研究SAP脚本,我想遍历excel中的每个单元格以检查单元格中是否包含日期,然后将日期转换为字符串,然后使用python将整个excel文件保存为文本。
import openpyxl
from datetime import date
wb=openpyxl.load_workbook("C:\\Users\\Nema\\AppData\\Local\\Temp\\export.xlsx")
ds = datetime()
for cell in wb:
if cell.value = ds
print('Done')
预先感谢
这应该循环遍历值所在区域中的所有excel单元格,找到所有“日期”单元格,并通过将它们转换为字符串格式来更新输入excel)>
从您的问题尚不清楚,所需的结果是txt还是excel文件,但不会有太大变化
import openpyxl import datetime file_name = 'export.xlsx' wb = openpyxl.load_workbook(file_name) # using ds will check against timestamp # using today will check only the date (not time) ds = datetime.datetime.now() today = datetime.date.today() for worksheet in wb: # loop through worksheets # loop through rows (only rows with content are considered) for row in worksheet.iter_rows(): # loop through row cells for cell in row: # check if is date and if required value date if cell.is_date and cell.value.date() == today: # convert into string # here you can convert the date in the format needed cell.value = str(today) print('Done') # update the input excel wb.save(file_name)
输入excel示例:
结果是将单元格A1转换为字符串格式(这只是一个示例,您可以用任何方式对其进行格式化)。阶梯(字体,颜色)仍然保留