tksheet - Copy and Paste with headers

aapost aapost at idontexist.club
Sun Apr 16 10:24:34 EDT 2023


On 4/14/23 14:33, angela vales wrote:
> I have recently created a tkinter app and need the ability to copy and paste data from tksheet table into an Excel file. I do have a button for export, but it will be beneficial to also allow the user to simply copy,paste.
> 
> I have enabled the appropriate bindings but cannot find a solution to also
> copy the header information during the copy and paste.

the csv export code runs through a different path than the ctrl_c code, 
one operating on the sheet level, one on the main table level (I didn't 
dig in to the depths but my assumptions would be that main table doesn't 
mathematically consider the headers in the same way).

def yield_sheet_rows in _tksheet.py vs def ctrl_c in _tksheet_main_table.py

Comparing how the each path functions, without a larger redesign of 
tksheet, you could create a custom button press combo binding to 
something other than ctrl-c utilizing the yield_sheet_rows (or - 
disallow all other ctrl-c functionality in favor of ONLY a csv style 
everything dump when using ctrl-c):

Import these:
import csv as csv_module
import io

This would be your custom binding functionality:
rows = self.sheet.yield_sheet_rows(get_header = True, get_index = False)

s = io.StringIO()
writer = csv_module.writer(s, dialect = csv_module.excel_tab, 
lineterminator = "\n")
for row in rows:
     writer.writerow(row)
self.clipboard_clear()
self.clipboard_append(s.getvalue())

It would need something deeper if you wanted to integrate it to ctrl-c 
and keep the existing ctrl-c functionality




More information about the Python-list mailing list