[reportlab-users] Table and document widths

Will Newton reportlab-users@reportlab.com
Thu, 8 Jan 2004 13:28:37 +0000


--KsGdsel6WgEHnImy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Tue, Jan 06, 2004 at 05:32:14PM +0000, Robin Becker wrote:

> well you can assume that the doc width is 12 points too large if you
> want to do percentages. Unfortunately we don't yet allow setting the
> widths as percentages so you'd have to do this yourself. 

Tha attached patch implements this. It's pretty simple so may well
miss something important. It doesn't check to make sure you have a
total of 100% or anything like that.

--KsGdsel6WgEHnImy
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="pc-column-widths.diff"

--- reportlab/platypus/tables.py	Wed Jul  9 09:17:44 2003
+++ reportlab.mod/platypus/tables.py	Thu Jan  8 12:27:46 2004
@@ -358,6 +358,16 @@
     def _calc(self, availWidth, availHeight):
         if hasattr(self,'_width'): return
 
+        newColWidths = []
+        for colWidth in self._colWidths:
+            if type(colWidth) == type(""):
+                percentage = float(colWidth.replace("%",""))
+                int_colwidth = int(percentage/100.0 * availWidth)
+                newColWidths.append(int_colwidth)
+            else:
+                newColWidths.append(colWidth)
+        self._colWidths = newColWidths
+        self._argW = newColWidths
         #in some cases there are unsizable things in
         #cells.  If so, apply a different algorithm
         #and assign some withs in a dumb way.
--- reportlab/test/test_table_layout.py	Mon Apr 21 23:27:49 2003
+++ reportlab.mod/test/test_table_layout.py	Thu Jan  8 13:20:50 2004
@@ -90,6 +90,14 @@
         lst.append(t1)
         lst.append(Spacer(18,18))
 
+        pc_colwidths = ("11.5%", 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32)
+        t1a = Table(data, pc_colwidths, rowheights)
+        t1a.setStyle(GRID_STYLE)
+        lst.append(Paragraph("This is GRID_STYLE with percentage and explicit column widths.  Each cell contains a string or number\n", styleSheet['BodyText']))
+        lst.append(t1a)
+        lst.append(Spacer(18,18))
+
+
         t2 = Table(data, None, None)
         t2.setStyle(GRID_STYLE)
         lst.append(Paragraph("""This is GRID_STYLE with no size info. It
@@ -339,6 +347,14 @@
         a paragraph when a string will do.
         """, styNormal))
 
+        data = [["Foo", "Bar", "Baz", 100],
+                [100, "Tree", "Hedge", "House"]]
+        pc_colwidths = ("10.5%", "60%", "20%", None)
+        t = Table(data, pc_colwidths)
+        t.setStyle(GRID_STYLE)
+        lst.append(Paragraph("This is GRID_STYLE with percentage and implicit column widths.  Each cell contains a string or number\n", styleSheet['BodyText']))
+        lst.append(t)
+
         SimpleDocTemplate('test_table_layout.pdf', showBoundary=1).build(lst)
 
 def makeSuite():
@@ -350,4 +366,4 @@
     unittest.TextTestRunner().run(makeSuite())
     print 'saved test_table_layout.pdf'
 
-    
\ No newline at end of file
+    

--KsGdsel6WgEHnImy--