Skip to content

Instantly share code, notes, and snippets.

@danizen
Created May 18, 2021 22:39
Show Gist options
  • Save danizen/66788e4e6bf5b0d8f14a71e37d677751 to your computer and use it in GitHub Desktop.
Save danizen/66788e4e6bf5b0d8f14a71e37d677751 to your computer and use it in GitHub Desktop.
import attr
@attr.s
class BibData:
mms_id = attr.ib()
title = attr.ib()
status = attr.ib()
mod_date = attr.ib()
nlm_unique_id = attr.ib(default='')
sef = attr.ib(default=False)
last_updated = attr.ib(default='')
last_status = attr.ib(default='')
what_changed = attr.ib(default='')
def to_list(self):
return [
str(self.mms_id),
str(self.nlm_unique_id),
self.title,
self.status,
self.mod_date,
'Yes' if self.sef else 'No',
self.last_updated,
self.last_status,
self.what_changed,
]
import openpyxl
from openpyxl.worksheet.table import Table, TableStyleInfo
def write_xlsx(path, records, must_have_nlmui=False):
wb = openpyxl.Workbook()
ws = wb.active
# Write header row
ws.append([
'MMS Id',
'NLM Unique Id',
'Title',
'Lifecycle',
'Modification Date',
'In SEF',
'005 in Export',
'Status in Export',
'What changed',
])
# write rows
for record in records.values():
if must_have_nlmui and not record.nlm_unique_id:
continue
ws.append(record.to_list())
# resize columns (worked this out manually)
ws.column_dimensions['A'].width = 20
ws.column_dimensions['B'].width = 20
ws.column_dimensions['C'].width = 44
ws.column_dimensions['D'].width = 16
ws.column_dimensions['E'].width = 20
ws.column_dimensions['F'].width = 8
ws.column_dimensions['G'].width = 16
ws.column_dimensions['H'].wdith = 20
ws.column_dimensions['I'].width = 44
# insert table
tab = Table(displayName='Table1', ref=f'A1:I{ws.max_row}')
style = TableStyleInfo(name='TableStyleMedium9', showFirstColumn=False,
showLastColumn=False, showRowStripes=True, showColumnStripes=False)
tab.tableStyleInfo = style
ws.add_table(tab)
# save
wb.save(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment