tablepyxl.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Do imports like python3 so our package works for 2 and 3
  2. from __future__ import absolute_import
  3. from lxml import html
  4. from openpyxl import Workbook
  5. from openpyxl.utils import get_column_letter
  6. from premailer import Premailer
  7. from tablepyxl.style import Table
  8. def string_to_int(s):
  9. if s.isdigit():
  10. return int(s)
  11. return 0
  12. def get_Tables(doc):
  13. tree = html.fromstring(doc)
  14. comments = tree.xpath('//comment()')
  15. for comment in comments:
  16. comment.drop_tag()
  17. return [Table(table) for table in tree.xpath('//table')]
  18. def write_rows(worksheet, elem, row, column=1):
  19. """
  20. Writes every tr child element of elem to a row in the worksheet
  21. returns the next row after all rows are written
  22. """
  23. from openpyxl.cell.cell import MergedCell
  24. initial_column = column
  25. for table_row in elem.rows:
  26. for table_cell in table_row.cells:
  27. cell = worksheet.cell(row=row, column=column)
  28. while isinstance(cell, MergedCell):
  29. column += 1
  30. cell = worksheet.cell(row=row, column=column)
  31. colspan = string_to_int(table_cell.element.get("colspan", "1"))
  32. rowspan = string_to_int(table_cell.element.get("rowspan", "1"))
  33. if rowspan > 1 or colspan > 1:
  34. worksheet.merge_cells(start_row=row, start_column=column,
  35. end_row=row + rowspan - 1, end_column=column + colspan - 1)
  36. cell.value = table_cell.value
  37. table_cell.format(cell)
  38. min_width = table_cell.get_dimension('min-width')
  39. max_width = table_cell.get_dimension('max-width')
  40. if colspan == 1:
  41. # Initially, when iterating for the first time through the loop, the width of all the cells is None.
  42. # As we start filling in contents, the initial width of the cell (which can be retrieved by:
  43. # worksheet.column_dimensions[get_column_letter(column)].width) is equal to the width of the previous
  44. # cell in the same column (i.e. width of A2 = width of A1)
  45. width = max(worksheet.column_dimensions[get_column_letter(column)].width or 0, len(table_cell.value) + 2)
  46. if max_width and width > max_width:
  47. width = max_width
  48. elif min_width and width < min_width:
  49. width = min_width
  50. worksheet.column_dimensions[get_column_letter(column)].width = width
  51. column += colspan
  52. row += 1
  53. column = initial_column
  54. return row
  55. def table_to_sheet(table, wb):
  56. """
  57. Takes a table and workbook and writes the table to a new sheet.
  58. The sheet title will be the same as the table attribute name.
  59. """
  60. ws = wb.create_sheet(title=table.element.get('name'))
  61. insert_table(table, ws, 1, 1)
  62. def document_to_workbook(doc, wb=None, base_url=None):
  63. """
  64. Takes a string representation of an html document and writes one sheet for
  65. every table in the document.
  66. The workbook is returned
  67. """
  68. if not wb:
  69. wb = Workbook()
  70. wb.remove(wb.active)
  71. inline_styles_doc = Premailer(doc, base_url=base_url, remove_classes=False).transform()
  72. tables = get_Tables(inline_styles_doc)
  73. for table in tables:
  74. table_to_sheet(table, wb)
  75. return wb
  76. def document_to_xl(doc, filename, base_url=None):
  77. """
  78. Takes a string representation of an html document and writes one sheet for
  79. every table in the document. The workbook is written out to a file called filename
  80. """
  81. wb = document_to_workbook(doc, base_url=base_url)
  82. wb.save(filename)
  83. def insert_table(table, worksheet, column, row):
  84. if table.head:
  85. row = write_rows(worksheet, table.head, row, column)
  86. if table.body:
  87. row = write_rows(worksheet, table.body, row, column)
  88. def insert_table_at_cell(table, cell):
  89. """
  90. Inserts a table at the location of an openpyxl Cell object.
  91. """
  92. ws = cell.parent
  93. column, row = cell.column, cell.row
  94. insert_table(table, ws, column, row)