Load an excel file into DuckDB database using Python

A short example of how to load data that is in a sheet in an excel file into a DuckDB database saved on disk using python.

# A simple python script to load an excel file into 
# a duckdb database

import duckdb

# use a persistant database file on disk
con = duckdb.connect('data/data.duckdb')


# use the sheet with name 'Sheet1'
# all data will be loaded in as strings "all_varchar=Tru"
# can be cast to appropriate data types later in pipeline
con.sql("""
    CREATE OR REPLACE TABLE raw_data AS
    SELECT * FROM read_xlsx('excel_file_path.xlsx', 
                             sheet='Sheet1', 
                             all_varchar=True)
""")

# close database connection
con.close() 

Send a Comment

Your email address will not be published.