Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from reportlab.lib.randomtext import chomsky
- from reportlab.platypus.tables import Table, GRID_STYLE
- from reportlab.platypus import SimpleDocTemplate, LayoutError
- from reportlab.lib import pagesizes
- import random
- def split_heights(vs, availableHeight):
- res = []
- for v in vs:
- if sum(res) + v < availableHeight:
- res.append(v)
- else:
- yield res
- res = [v]
- yield res
- def split_data(data, rowHeights):
- i = 0
- for item in rowHeights:
- chunk = []
- for _ in item:
- chunk.append(data[i])
- i += 1
- yield chunk
- def phase1(data, dest):
- template = SimpleDocTemplate(dest, pagesize=pagesizes.A4)
- inner_table = Table(data, style=GRID_STYLE)
- outer_table = Table([['Sequences', inner_table]], style=GRID_STYLE)
- flowables = [outer_table]
- try:
- template.build(flowables)
- except LayoutError as e:
- rowHeights = inner_table._rowHeights
- frame = template.frame
- availableHeight = frame._aH
- return list(split_heights(rowHeights, availableHeight))
- def phase2(data, dest, rowHeights):
- tables = []
- for chunk in split_data(data, rowHeights):
- tables.append(Table(chunk, style=GRID_STYLE))
- data_ = [['Sequences', table] for table in tables]
- flowables = [Table(data_, style=GRID_STYLE)]
- template = SimpleDocTemplate(dest, pagesize=pagesizes.A4)
- template.build(flowables)
- def main():
- data = [[chomsky(random.randint(2,5))] for c in range(10)]
- dest = "test1.pdf"
- rowHeights = phase1(data, dest)
- if rowHeights is None:
- return
- phase2(data, dest, rowHeights)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement