无法生成表格

时间:2018-03-03 23:22:59

标签: python reportlab

我有一个像这样的numpy数组:

[['aaq', '56.18%', 'CC']
 ['jasl', '0.00%', 'JO']
 ['arw', '1.74%', 'AS']
 ['vcf', '1.07%', 'DA']
 ['hsw', '0.00%', 'LO']
 ['wey', '41.01%', 'IS']
 ['fol', '0.00%', 'MO']
 ['aaw', '0.00%', 'AP'],
 ['sta', '0.00%', 'TT']]

我想用作表格的数据。 为了做到这一点,我所尝试的是:

doc = SimpleDocTemplate("sectors.pdf",pagesize=A4)
elements = []

data=    [['aaq', '56.18%', 'CC']
 ['jasl', '0.00%', 'JO']
 ['arw', '1.74%', 'AS']
 ['vcf', '1.07%', 'DA']
 ['hsw', '0.00%', 'LO']
 ['wey', '41.01%', 'IS']
 ['fol', '0.00%', 'MO']
 ['aaw', '0.00%', 'AP'],
 ['sta', '0.00%', 'TT']]

t=Table(data)    

elements.append(t)
doc.build(elements)

当我运行代码时,我收到以下错误:

Traceback (most recent call last):
  File "sector_alloc.py", line 236, in <module>
    t=Table(data)
  File "reportlab/platypus/tables.py", line 212, in __init__
raise ValueError("%s invalid data type" % self.identity())
ValueError: <Table@0x7F61F6D2ACC0 unknown rows x unknown cols>...     
invalid data type

这可能与将np.array的值转换为Paragraphs有关吗? (也许是因为它们在数据中是str) 在这种情况下,我所做的是分别对np.array的每一列(list)进行操作并执行:

for valor in list:
  list.append(Paragraph('<b>'+valor+'</b>',styleSheet["BodyText"]))

然后尝试再次连接lists以再次形成numpy数组,但也没有工作..

有人可以帮我把numpy数组作为我桌子的数据吗?

1 个答案:

答案 0 :(得分:1)

在数据列表中的大多数列表之后,您似乎缺少逗号。列表列表应如下所示:

    @IBAction func doSegue(_ sender: UIButton) {
        buttonTag = sender.tag

        let storyboard = UIStoryboard (name: "Main", bundle: nil)
        let resultVC = storyboard.instantiateViewController(withIdentifier: "ResultViewController")as! ResultViewController

        // Communicate with new VC - These values are stored in the destination
        // you can set any value stored in the destination VC here
        resultVC.firstValue = buttonTag
        resultVC.secondValue = randomOpponentValue()
        self.navigationController?.pushViewController(resultVC, animated: true)
    }

以下是我测试的完整示例:

data = [
    ['aaq', '56.18%', 'CC'],
    ['jasl', '0.00%', 'JO'],
    ['arw', '1.74%', 'AS'],
    ['vcf', '1.07%', 'DA'],
    ['hsw', '0.00%', 'LO'],
    ['wey', '41.01%', 'IS'],
    ['fol', '0.00%', 'MO'],
    ['aaw', '0.00%', 'AP'],
    ['sta', '0.00%', 'TT'],
]