将打印功能写入Excel文件

时间:2019-05-10 18:59:04

标签: python

(python的新手)

我想将打印功能写入正在创建的excel文件中。知道怎么做吗?

我希望以下函数显示在我的表格中 1)开始时间 2)已保存 3)退出 4)结束时间 5)经过的时间 6)文件名

下面是我正在运行的代码。

我正在运行脚本,然后创建带有日期和时间戳的excel文件

import win32com.client
import time
import datetime
import os
import xlsxwriter

start_time = time.time()
datetime.datetime.now()
print ("--- %s Start time ---" % (datetime.datetime.now()))
datetime.datetime(2009,1,6,15,8,24,78915)

try:


    flpth = r"filepath\Test File.xlsx"
    xlapp = win32com.client.DispatchEx("Excel.Application")
    wb = xlapp.Workbooks.Open(flpth )
    xlapp.displayalerts = 0
    xlapp.visible = 1
    wb.RefreshAll()
    time.sleep(5)
    wb.save
    print("---  Saved ---")
    xlapp.Quit()
    print("---  Quit ---")

    end_time = time.time()


#print the datetime / diff / file path name 
    print ("--- %s End time ---" % (datetime.datetime.now()))
    print ("--- %s Seconds ---" % round(time.time() - start_time))
    print("--- %s ---" % os.path.splitext(os.path.basename("Test File.xlsx"))[0])



# Create a workbook (with date-time) and add a worksheet.
    todays_date = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M") )+ '.xlsx'
    todays_date = todays_date.replace(" ", "_").replace(":", "_")

    workbook = xlsxwriter.Workbook (todays_date)
    worksheet = workbook.add_worksheet('Report Summary')

# Make Columns Large Enough for Text.
    worksheet.set_column('A:A', 20)
    worksheet.set_column('B:B', 20)
    worksheet.set_column('C:C', 20)
    worksheet.set_column('D:D', 20)
    worksheet.set_column('E:E', 20)
    worksheet.set_column('E:E', 20)
    worksheet.set_column('F:F', 20)

# Column Headers
    worksheet.write('A1', 'Start Time')
    worksheet.write('B1', 'File Saved')
    worksheet.write('C1', 'File Quit')
    worksheet.write('D1', 'End Time')
    worksheet.write('E1', 'Time Elapsed')
    worksheet.write('F1', 'File Name')

# Write some numbers, with row/column notation.
    worksheet.write(2, 0, 123)
    worksheet.write(3, 0, 123.456)

    workbook.close()
    print ('close')



    wb = None
    xlapp = None


except:
    wb = None
    xlapp = None

1 个答案:

答案 0 :(得分:1)

我不完全了解您对程序的意图。我认为,与电子表格进行交互的高级模块会很好。仅举几例,其中包括pandasopenpyxlxlwings。 python安装中还附带了一个标准的csv模块。我个人更喜欢xlwings来完成我的电子表格的大部分工作(您将需要pip install xlwings),并且它还可以根据用例与pandas进行交互。演示简单用例的脚本如下所示:

import xlwings
import time

wb = xlwings.Book('test.xlsx')
sheet = wb.sheets['Sheet1']

start_time = time.time()
# make our headers
sheet.range("A1").value = "Start Time"
sheet.range("B1").value = "Saved"
sheet.range("C1").value = "Quit"
sheet.range("D1").value = "End Time"
sheet.range("E1").value = "Time Elapsed"
sheet.range("F1").value = "File Name"

row = 2
while True:
    sheet.range("A{}".format(row)).value = start_time
    sheet.range("B{}".format(row)).value = "No"
    sheet.range("C{}".format(row)).value = "No"
    now = time.time()
    sheet.range("D{}".format(row)).value = now
    sheet.range("E{}".format(row)).value = now - start_time
    sheet.range("F{}".format(row)).value = "Some name"
    row += 1
    time.sleep(1)

几秒钟后的输出示例如下:

output worksheet

我认为,如果您想获得更合适的解决方案,您需要在问题上更具体地说明要做什么。