如何解决使用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 举报,一经查实,本站将立刻删除。