Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to import data from database to excel
#1
I am using microsoft access database.
I want to copy data from the table with column names and paste it into a excel file.
I try to copy but I got some errors

import openpyxl as op
import pyodbc

con = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=path;')
cr = con.cursor()



loc = (r'path of the excel file')

wb = op.load_workbook(loc)
sheet = wb.create_sheet(0)
sheet.title="Trial"

cr.execute('select * from  detail')
result=list(cr.fetchall())

sheet.append(cr.column_name)

for row in cr.columns(table='detail'):
    sheet.append(row.column_name)

for row in result:
    sheet.append(row)


Error:
Traceback (most recent call last): File "C:/Users/Nick/PycharmProjects/py/venv/excel.py", line 21, in <module> sheet.append(rows.column_name) File "C:\Users\Nick\PycharmProjects\py\venv\lib\site-packages\openpyxl\worksheet\worksheet.py", line 661, in append self._invalid_row(iterable) File "C:\Users\Nick\PycharmProjects\py\venv\lib\site-packages\openpyxl\worksheet\worksheet.py", line 792, in _invalid_row type(iterable)) TypeError: Value must be a list, tuple, range or generator, or a dict. Supplied value is <class 'str'>
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  iterate read data from excel sheet jp2017 1 66 Jun-19-2019, 07:45 PM
Last Post: micseydel
  Using executemany to import the data Sandy7771989 1 93 Jun-11-2019, 07:45 AM
Last Post: Sandy7771989
  Connect to database OVH Serferi 0 133 Apr-26-2019, 07:34 AM
Last Post: Serferi
  Python validate excel values data types Useruser00 0 138 Apr-08-2019, 01:29 PM
Last Post: Useruser00
  Who converts data when writing to a database with an encoding different from utf8? AlekseyPython 1 193 Mar-04-2019, 08:26 AM
Last Post: DeaD_EyE
  How to update sql database from csv Prince_Bhatia 0 172 Feb-09-2019, 09:15 PM
Last Post: Prince_Bhatia
  Data in excel Hass 1 268 Nov-30-2018, 03:59 PM
Last Post: nilamo
  Dataframe Data Query Database ab0217 0 385 Oct-16-2018, 02:26 AM
Last Post: ab0217
  data from excel mida1 0 369 Oct-12-2018, 02:02 PM
Last Post: mida1
  Data import from a graph on the web NataliyaDem 1 356 Sep-26-2018, 04:22 PM
Last Post: Axel_Erfurt

Forum Jump:


Users browsing this thread: 1 Guest(s)