使用Pyodbc和openpyxl格式化数据/时间

问题描述

我有一个Python脚本,该脚本使用SQL查询从.MDB中获取记录,并将其打印到excel文档中。打印的两个字段是日期/时间字段。在脚本中,我应该将转换的date.time部分放在哪里?那会是什么样子?以下是脚本的excel部分。我需要格式为YYYY / MM / DD,时间为xx:xx。谢谢!

import pyodbc
from platform import python_version
pyVersion = str(python_version())

if (pyVersion[0:3] == "2.7"):
    from Tkinter import *
    import Tkinter,Tkconstants,tkFileDialog,tkMessageBox
if (pyVersion[0] == "3"):
    from tkinter import filedialog,messageBox
    from tkinter import * 

import os
import glob
from openpyxl import Workbook
import datetime

if len(lstDuplicates) > 0:

    dirName = os.path.dirname(mdbPath.get())
    mdbname = os.path.basename(mdbPath.get())
    xlfilepath = os.path.join (dirName,mdbname.split('.')[0] + "_Duplicates.xlsx")
    print(xlfilepath)
    wb = Workbook()
    ws = wb.active
    ws.title = "Duplicates"
    wb.save(xlfilepath)

    ws['A1'] = "DuplicateRecords"
    ws['B1'] = "Purpose"
    ws['C1'] = "Length Surveyed"
    ws['D1'] = "Direction"
    ws['E1'] = "Inspector"
    ws['F1'] = "Date"
    ws['G1'] = "Time"

    for x in range(len(lstDuplicates)):
        ws['A' + str(x + 2)] = lstDuplicates[x]

    for x in range(len(lstPurpose)):
        ws['B' + str(x + 2)] = lstPurpose[x]

    for x in range(len(lstLength)):
        ws['C' + str(x + 2)] = lstLength[x]
    for x in range(len(lstDirection)):
        ws['D' + str(x + 2)] = lstDirection[x]
    for x in range(len(lstInspector)):
        ws['E' + str(x + 2)] = lstInspector[x]
    for x in range(len(lstDate)):
        ws['F' + str(x + 2)] = lstDate[x]
    for x in range(len(lstTime)):
        ws['G' + str(x + 2)] = lstTime[x]


    wb.save(xlfilepath)


return

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)