1

I have a treeview that is populated from a database.

I want to have a button that exports all the data, or some selective(based on the NOT NULL structure) to an .xlsx file type.

Ideally, I would like to have a central directory to store these files as exported and being able to edit said files and read them at a later stage, obviously upon requirement.

The code of information to populate the treeview from .db is as follows:

from tkinter import *
from tkinter import ttk
import sqlite3
from tkinter.ttk import *
import os

# Tkinter Create and Layout
root = Tk()
# root.state('zoomed')
root.title("Inventory Balance")
root.config(bg="skyblue2")

# Information from Database
def stock_sheet():
    tree.delete(*tree.get_children())
    with sqlite3.connect('Test.sql3') as conn:
        mycursor = conn.cursor()
        mycursor.execute("SELECT * FROM StockSheet")
        for row in mycursor:
            tree.insert('', 'end', values=row[0:3])
frame = Frame(root)
frame.pack()

style = ttk.Style()
style.configure("Treeview.Heading", font=("Arial Narrow", 18, 'bold'), foreground='black')

tree = ttk.Treeview(frame, columns=(0, 1, 2), height=32, show="headings")
tree.pack(side='left')

tree.heading(0, text="Item Code")
tree.heading(1, text="Description")
tree.heading(2, text="Category")

tree.column(0, width=150)
tree.column(1, width=400)
tree.column(2, width=200)

# Scrollbar Layout and Configuration
scroll = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
scroll.pack(side='right', fill='y')

tree.configure(yscrollcommand=scroll.set)

I did try to shorten it for question purpose.

I would like to know how i can export the treeview from the tkinter window.

I do not have very sophisticated users in this sense.

I can not find any answers applicable at this stage and I am on quite a deadline.

All help appreciated.

ReflexTechR
  • 41
  • 1
  • 9

1 Answers1

0

I have searched through older posts and found the following answer VERY helpful:

How to export sqlite to excel Please check answer of user(Credit to: Farhan Khan)

I focused on exporting the .db rather than exporting the treeview data.

I just adopted it for my needs.

Just a brief update on how I got a solution for future possible viewers with a similar issue.

ReflexTechR
  • 41
  • 1
  • 9