{"id":25640525,"url":"https://github.com/kstrauser/tablefactory","last_synced_at":"2025-04-15T02:40:56.820Z","repository":{"id":1392474,"uuid":"1383117","full_name":"kstrauser/tablefactory","owner":"kstrauser","description":"Easily create HTML, spreadsheet, or PDF tables from common Python data sources","archived":false,"fork":false,"pushed_at":"2013-05-27T23:52:37.000Z","size":388,"stargazers_count":18,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T17:38:51.523Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://kstrauser.github.com/tablefactory/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"moodleou/moodle-qtype_ddmarker","license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kstrauser.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-02-18T16:32:11.000Z","updated_at":"2021-03-29T10:29:11.000Z","dependencies_parsed_at":"2022-08-16T13:15:17.596Z","dependency_job_id":null,"html_url":"https://github.com/kstrauser/tablefactory","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kstrauser%2Ftablefactory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kstrauser%2Ftablefactory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kstrauser%2Ftablefactory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kstrauser%2Ftablefactory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kstrauser","download_url":"https://codeload.github.com/kstrauser/tablefactory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248996744,"owners_count":21195772,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-23T04:40:07.851Z","updated_at":"2025-04-15T02:40:56.801Z","avatar_url":"https://github.com/kstrauser.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"TableFactory is a very simple interface for creating report tables from data\r\nsets you provide. It uses other projects for most of the heavy lifting:\r\nReportLab makes PDFS and xlwt makes spreadsheets. It's especially well\r\nsuited to adding reporting capabilities to your Pyramid, TurboGears, Pylons,\r\nor Django projects.\r\n\r\n\u003e \"For this, we found the TableFactory API developed by Kirk Strauser is\r\n\u003e very much evolved and beautiful.\r\n\u003e\r\n\u003e For a Python novice like me, you helped me bypass the labyrinth of\r\n\u003e ReportLab and xlwt. Thanks again!\" - Swara Technologies\r\n\r\n# Motivation\r\n\r\nI maintain a website that provides many custom reports to its users, and\r\nmost of them need to be available in several different output formats.\r\nAlmost all of those reports follow the same pattern:\r\n\r\n1. Run a database query,\r\n2. Reformat the data slightly as needed, and\r\n3. Return a (usually) simple grid of a few columns from each of those rows.\r\n\r\nTableFactory addresses #3. Some customers are content to view their data in\r\ntheir web browser, while others want print-ready PDFs and still others want\r\nto edit it in a spreadsheet program. I needed an easy-to-use wrapper around\r\nthe other reporting backends so that I could configure a report one time and\r\nthen publish it in any desired format. ReportLab is astoundingly powerful,\r\nbut these simple little tables only use a fraction of its power. The same is\r\ntrue for xlwt: it can do many amazing things that I never need it to do.\r\n\r\nThis is where TableFactory comes in. It's not as flexible as either of those\r\nprojects, but it does all the tedious, repetitive, and fragile work of\r\ngetting the data ready for output and building the layout of the finished\r\ntables.\r\n\r\n# Example\r\n\r\nSuppose I want to build a table listing the customer name and total amount\r\nfrom some invoices in our database.  My company uses SQLAlchemy and I have\r\nan \"Invoice\" class that maps to our invoice table. This fetches the first\r\nten rows from that table:\r\n\r\n    invoices = session.query(Invoice).limit(10)\r\n\r\nI'm finished with step #1 above. This is a simple report and I'll skip the\r\nsecond step and go straight to generating the output.\r\n\r\nFirst, I'll build a \"row specification\" object that contains information\r\nabout the columns in the report. Each \"column specification\" lists the name\r\nof a column from an invoice table row and its human-readable name. The\r\ncustomer wants the invoice amounts to stand out, so I'll make them bold:\r\n\r\n    rowmaker = RowSpec(ColumnSpec('customer', 'Customer'),\r\n                       ColumnSpec('invamt', 'Invoice Amount', bold=True))\r\n\r\nTableFactory classes work on \"table row\" objects. The RowSpec instance I\r\njust made can convert those SQLAlchemy results into TableRows:\r\n\t\t       \r\n    lines = rowmaker.makeall(invoices)\r\n    \r\nBehind the scenes, it loops across all of the objects in \"invoices\" and\r\nconverts the \"customer\" and \"invamt\" columns into table cells. Next, I'll\r\ncreate the table builder:\r\n\r\n    pdfmaker = PDFTable('Invoice amounts by customer', headers=rowmaker)\r\n    \r\nThis will give us a PDF titled \"Invoice amounts by customer\" with columns\r\ntitled \"Customer\" and \"Invoice Amount\" (pulled from the RowSpec I made a\r\ncouple of steps ago!). Finally, to assemble the PDF and write it to a file:\r\n    \r\n    open('invoicetable.pdf', 'wb').write(pdfmaker.render(lines))\r\n    \r\nOur PDFTable's \"render\" method accepts the TableRows I made earlier and\r\nturns them into a PDF. That's it! I'm done and ready to go home for the day.\r\n\r\nBut wait! The customer's accounting department needs an Excel spreadsheet\r\nthey can import into their own database. Conveniently, I've already done all\r\nthe \"hard\" work and only need to make a new spreadsheet generator:\r\n\r\n    sheet = SpreadsheetTable('Invoice amounts by customer', headers=rowmaker)\r\n    open('invoicetable.xls', 'wb').write(sheet.render(lines))\r\n\r\nAnd with that, it's time to go on a break.\r\n\r\n# License\r\n\r\nTableFactory is available under the permissive MIT License.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkstrauser%2Ftablefactory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkstrauser%2Ftablefactory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkstrauser%2Ftablefactory/lists"}