【问题标题】:Build reportlab table without using flowables在不使用 flowables 的情况下构建 reportlab 表
【发布时间】:2018-12-28 12:27:22
【问题描述】:

我想在 pdf 中绘制一个报告实验室表。有没有办法将表格绘制到特定的(x,y)坐标?我没有使用任何流动性。我只想指定表格的位置。 对于绘制图表,我使用“renderPDF.draw()”。这不适用于表格。

谢谢!

【问题讨论】:

    标签: python reportlab


    【解决方案1】:

    我找到了解决办法:

    canv = Canvas("phello.pdf")    
    
    data = [['00', '01', '02', '03', '04'],
            ['10', '11', '12', '13', '14'],
            ['20', '21', '22', '23', '24'],
            ['30', '31', '32', '33', '34'],
            ['20', '21', '22', '23', '24'],
            ['20', '21', '22', '23', '24'],
            ['20', '21', '22', '23', '24'],
            ['20', '21', '22', '23', '24']]
    t = Table(data)
    
    t.wrap(0, 0) # not sure why this is necessary
    t.drawOn(canv, 72, 200) # parameters specify the location of the table
    canv.save()
    

    问候

    【讨论】:

      【解决方案2】:

      如果其他人回答这个问题。

      我正在寻找一种从鸭嘴兽中使用 Table 并添加到画布的方法,因为 TableWidget() 不适合我,并想出了以下代码,该代码是从以下位置重新调整的: https://www.blog.pythonlibrary.org/2010/09/21/reportlab-tables-creating-tables-in-pdfs-with-python/

      显示更多的说明

      from reportlab.lib import colors
      from reportlab.lib.pagesizes import letter
      from reportlab.lib.units import inch, mm
      from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
      
      c = canvas.Canvas('testing.pdf', pagesize=A4)
      c.setFont('Helvetica', 12, leading=None)
      
      def add_table(self):
          
          data= [['00', '01', '02', '03', '04']
               , ['10', '11', '12', '13', '14']
               , ['20', '21', '22', '23', '24']
               , ['30', '31', '32', '33', '34']]
          t=Table(data
                  , 5*[0.4*inch] #width of 5 cols, size of []
                  , 4*[0.4*inch]) #height of 4 rows,  size of []
          
          # below shows which 
          t.setStyle(TableStyle([('ALIGN'    ,(1, 1),(-2,-2),'RIGHT')     #instead of -2,-2 you could do 3,2
                               , ('TEXTCOLOR',(1, 1),(-2,-2),colors.red)  #and again here 
                                 
                               , ('VALIGN'   ,(0, 0),( 0,-1),'TOP')
                               , ('TEXTCOLOR',(0, 0),( 0,-1),colors.blue)
                                 
                               , ('ALIGN'    ,(0,-1),(-1,-1),'CENTER')
                               , ('VALIGN'   ,(0,-1),(-1,-1),'MIDDLE')
                               , ('TEXTCOLOR',(0,-1),(-1,-1),colors.green)
                                 
                               , ('INNERGRID',(0, 0),(-1,-1), 0.25, colors.black)
                               , ('BOX'      ,(0, 0),(-1,-1), 0.25, colors.black)
          ]))
          t.wrapOn(c, 100*mm, 180*mm) #wrap the table to this width, height in case it spills
          t.drawOn(c, 20*mm, 250*mm) #draw it on our pdf at x,y
      
      # for those like me who didn't know, * by inch or mm is because reportlab uses 
      # something else to render and if you look at an A4 it has digits:
      #                                   (595.2755905511812, 841.8897637795277) 
      
          
      add_table(c)
      c.showPage()
      c.save()
      

      生成这样的纯 pdf:

      【讨论】:

        猜你喜欢
        • 2018-10-03
        • 2011-08-02
        • 1970-01-01
        • 1970-01-01
        • 2011-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-01
        相关资源
        最近更新 更多