如何使用Pandas为excel文档添加时间戳?

时间:2015-07-15 23:08:50

标签: python excel pandas ffmpeg

我有一个脚本,它使用FFMPEG和CMD逐行剪切excel文档中的视频文件。我希望python在完成一行后添加一个时间戳。你能帮忙吗?

import subprocess as sp, pandas as pd

ffmpeg = 'C:/FFMPEG/bin/ffmpeg.exe' # on Windows
datafile = r'C:\Users\A_Do\Dropbox\1. Projects\2. Python\TM Creator\tm_creator_test1.xlsx'

xl = pd.ExcelFile(datafile,index = False)
df = xl.parse('Sheet1')

def create_tm():
    row_iterator = df.iterrows()
     # take first item from row_iterator
    for i, row in row_iterator:
        infile = row['filename']
        outputfile = row['outputfilename']
        timein = row['timein']
        duration = row['duration']
        decision = row['Create TM?']
        if decision == "Y":
            sp.call(ffmpeg + " -y -i " + infile + " -map 0:0 -map 0:1 -map 0:2 -acodec copy -ss " + str(timein) + " -codec copy -t " + str(duration) + " " + outputfile,shell=True) #this works

        elif decision != decision: #this gets rid of the NaN
            break
        else:
            print "You said you didn't want to make a TM for " + str(infile)

create_tm()

谢谢!

我的最终代码:

import subprocess as sp, pandas as pd
# (1) new import
from openpyxl import load_workbook
# (2) new import
from datetime import datetime

ffmpeg = 'D:/FFMPEG/bin/ffmpeg.exe' # on Windows
datafile = r'D:\Dropbox\1. Projects\2. Python\TM Creator\tm_creator_test1.xlsx'

# (3) open the file in openpyxl first:
book = load_workbook(datafile)

xl = pd.ExcelFile(datafile,index = False)
df = xl.parse('Sheet1')

def create_tm():
    row_iterator = df.iterrows()
     # take first item from row_iterator
    for i, row in row_iterator:
        infile = row['filename']
        outputfile = row['outputfilename']
        timein = row['timein']
        duration = row['duration']
        decision = row['Create TM?']
        if decision == "Y":
            sp.call(ffmpeg + " -y -i " + infile + " -map 0:0 -map 0:1 -acodec copy -ss " + str(timein) + " -codec copy -t " + str(duration) + " " + outputfile,shell=True) #this works
            # (4) Wherever in the code you want to put the timestamp:
            df.loc[i, 'Timestamp'] = str(datetime.now())
            # (5) This saves the sheet back into the original file, without removing
            # any of the old sheets.
            writer = pd.ExcelWriter(datafile)
            writer.book = book
            writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
            df.to_excel(writer, index=False)
            writer.save()
        elif decision != decision: #this gets rid of the NaN
            break
        else:
            print "You said you didn't want to make a TM for " + str(infile)

1 个答案:

答案 0 :(得分:0)

应用this问题的答案,这是您的代码的更新版本,其中包含我做出更改的评论:

import subprocess as sp, pandas as pd
# (1) new import
from openpyxl import load_workbook
# (2) new import
from datetime import datetime

ffmpeg = 'C:/FFMPEG/bin/ffmpeg.exe' # on Windows
datafile = r'C:\Users\A_Do\Dropbox\1. Projects\2. Python\TM Creator\tm_creator_test1.xlsx'

# (3) open the file in openpyxl first:
book = load_workbook(datafile)

xl = pd.ExcelFile(datafile,index = False)
df = xl.parse('Sheet1')

def create_tm():
    row_iterator = df.iterrows()
     # take first item from row_iterator
    for i, row in row_iterator:
        infile = row['filename']
        outputfile = row['outputfilename']
        timein = row['timein']
        duration = row['duration']
        decision = row['Create TM?']
        if decision == "Y":
            sp.call(ffmpeg + " -y -i " + infile + " -map 0:0 -map 0:1 -map 0:2 -acodec copy -ss " + str(timein) + " -codec copy -t " + str(duration) + " " + outputfile,shell=True) #this works

        elif decision != decision: #this gets rid of the NaN
            break
        else:
            print "You said you didn't want to make a TM for " + str(infile)

        # (4) Wherever in the code you want to put the timestamp:
        df.loc[i, 'Timestamp'] = str(datetime.now())

    # (5) This saves the sheet back into the original file, without removing
    # any of the old sheets.
    writer = pd.ExcelWriter(datafile)
    writer.book = book
    writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
    df.to_excel(writer)
    writer.save()

create_tm()

如果您想更好地控制时间戳的格式,请参阅datetime.datetime.strftime()的文档。此外,我写这个假设您的Excel文件有其他表,如果你丢失你会不高兴。如果情况并非如此,那么你不必做同样的事情。您可以忽略编号为1和3的更改,并将{5替换为df.to_excel(datafile)