Python Forum
Formatting Data/Time with Pyodbc and openpyxl
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formatting Data/Time with Pyodbc and openpyxl
#1
I have a python script that takes records from an .MDB using a sql query and prints them to a excel document. Two of the fields that are printed are date/time fields. where about in my script would I place the date.time portion of the conversion? and what would that look like? below is the excel portion of the script. I need the format to be YYYY/MM/DD and time to be xx:xx. Thanks!

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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using pyodbc&pandas to load a Table data to df tester_V 3 792 Sep-09-2023, 08:55 PM
Last Post: tester_V
  Formatting a date time string read from a csv file DosAtPython 5 1,249 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  openpyxl convert data to float jacklee26 13 5,930 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  Need help formatting dataframe data before saving to CSV cubangt 16 5,766 Jul-01-2022, 12:54 PM
Last Post: cubangt
  pyodbc gmerritt 8 2,873 Feb-21-2022, 07:21 PM
Last Post: gmerritt
  Real time data satyanarayana 3 23,072 Feb-16-2022, 07:46 AM
Last Post: satyanarayana
  How to read rainfall time series and insert missing data points MadsM 4 2,165 Jan-06-2022, 10:39 AM
Last Post: amdi40
  OpenPyXl formatting issues kpayney1 0 1,662 Nov-26-2021, 01:56 AM
Last Post: kpayney1
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 6,274 Mar-29-2021, 09:36 PM
Last Post: Larz60+
  Confusion about [date]time [formatting] Mark17 0 1,731 Dec-17-2020, 07:25 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020