0

我正在尝试在 Excel 列中的下一个空单元格中输入日期,但标题中出现错误。到目前为止,我有这个:

from win32com.client import Dispatch
import datetime
import pythoncom

#open workbook
xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Add(' path to workbook ')
ws = wb.Worksheets(1)

#initialize values
empty = False
row = 1
col = 1
now = datetime.datetime.now()
day = str(now.day,"/")
month = str(now.month,"/")
year = str(now.year)
val = ws.Cells(row,col).value

#input date into Excel
#loop until empty cell is found in column
while not empty:
    val = ws.Cells(row,col).value
    print("Searching for next empty cell...")
    if val == None:
        print("Found empty cell! Writing date...")
        ws.Cells(row,col).value = (day + month + year)
        empty = True
    row += 1

#macro to save workbook
xl.Run('Save')

#close and release processes
wb.Close(True)
xl.Quit()

pythoncom.CoUninitialize()

有人有想法么?

谢谢

编辑我试图不将日期用作 int 的原因,是因为当它被带入 Excel 时,它只会带来一天(即今天只会带来 17 日),而不是日期的其余部分。

4

1 回答 1

2

尝试

ws.Cells(row,col).value = datetime.datetime.now().strftime("%d/%m/%Y")

strftime以您喜欢的格式将日期转换为字符串,无需手动进行。

于 2013-09-17T10:30:46.743 回答