0

I am trying to show the DATE in a word document. However, the code broke with the following exception. (I believe I need to change the DATE to STRING

    d.add_run(date_implemented(Part_No_New_Part))
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\python_docx-0.8.7-py3.7.egg\docx\text\paragraph.py", line 37, in add_run
    run.text = text
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\python_docx-0.8.7-py3.7.egg\docx\text\run.py", line 163, in text
    self._r.text = text
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\python_docx-0.8.7-py3.7.egg\docx\oxml\text\run.py", line 104, in text
    _RunContentAppender.append_to_run_from_text(self, text)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\python_docx-0.8.7-py3.7.egg\docx\oxml\text\run.py", line 134, in append_to_run_from_text
    appender.add_text(text)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\python_docx-0.8.7-py3.7.egg\docx\oxml\text\run.py", line 142, in add_text
    self.add_char(char)
  File "C:\Program Files (x86)\Python37-32\lib\site-packages\python_docx-0.8.7-py3.7.egg\docx\oxml\text\run.py", line 156, in add_char
    elif char in '\r\n':
TypeError: 'in <string>' requires string as left operand, not datetime.datetime

Repl Closed

I am able to generate the document. However, this is my Output

Date implemented: (datetime.datetime(2011, 5, 31, 0, 0),)

I want my output to be like this:

Date Implemented: 5/31/2015


The functions are the following:

def date_implemented(Part_No_New_Part):
    dsn_tns = cx_Oracle.makedsn('Server_NAME', 'XXXXX', service_name='TEST')
    conn = cx_Oracle.connect(user=r'XXX', password='XXXX', dsn=dsn_tns)
    c = conn.cursor()
    w_sql =('SELECT RELEASED_DT FROM TABLE1 
       WHERE PART_NO = UPPER(TRIM(:Part_No_New_Part)) AND STATE =\'Released\'')
    c.execute(w_sql,[Part_No_New_Part])
    for result in c:
        if result ==' ':
            print(" ")
        else:
            print('')
    return result


def document(result):
        d = doc.add_paragraph()
    d.add_run('Date implemented: ').bold=True
    d.add_run(date_implemented(Part_No_New_Part))
JUANLUISSG
  • 63
  • 1
  • 8

1 Answers1

0

So you could either use

str(date_var_from_sql)

or if you want a custom format:

date_var_from_sql.strftime('%m/%d/%Y')

See this for the format string syntax for strftime

Joe D
  • 79
  • 4