Skip to content

Instantly share code, notes, and snippets.

@geekscrapy
Created May 27, 2020 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekscrapy/41efe13ae2390cb4a0956e7f9fbd5131 to your computer and use it in GitHub Desktop.
Save geekscrapy/41efe13ae2390cb4a0956e7f9fbd5131 to your computer and use it in GitHub Desktop.
from visidata import *
@VisiData.api
def save_xlsx(vd, p, vs, *vsheets):
import openpyxl
wb = openpyxl.Workbook()
wb.remove_sheet(wb['Sheet'])
for vs in vsheets or [vs]:
ws = wb.create_sheet(title=vs.name)
headers = [col.name for col in vs.visibleCols]
ws.append(headers)
for dispvals in vs.iterdispvals(format=False):
row = []
for v in dispvals.values():
if type(v) == date:
v = datetime.datetime.fromtimestamp(int(v.timestamp()))
elif not type(v) in [int,float,str]:
v = str(v)
row.append(v)
ws.append(row)
wb.active = ws
wb.save(filename=p)
status(f'{p} save finished')
@VisiData.api
def save_xls(vd, p, vs, *vsheets):
import xlwt
wb = xlwt.Workbook()
for vs in vsheets or [vs]:
ws1 = wb.add_sheet(vs.name[:32]) # sheet name can not be longer than 32 characters
for col_i, col in enumerate(vs.visibleCols):
ws1.write(0, col_i, col.name)
for r_i, dispvals in enumerate(vs.iterdispvals(format=False)):
r_i += 1
for c_i, v in enumerate(dispvals.values()):
if not type(v) in [int,float,str]:
v = str(v)
ws1.write(r_i, c_i, v)
wb.save(p)
status(f'{p} save finished')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment