Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Reportlab错误:';表';对象没有属性'_colpositions';_Python_Python 3.x_Reportlab - Fatal编程技术网

Python Reportlab错误:';表';对象没有属性'_colpositions';

Python Reportlab错误:';表';对象没有属性'_colpositions';,python,python-3.x,reportlab,Python,Python 3.x,Reportlab,我试图从用户指南中自学reportlab。我需要创建的文档只是格式化文本,需要放在页面的特定位置。在以下代码段中,表_数据是一个包含3个字符串的列表。我将表作为pdfTable导入,因为我的应用程序有一个表类 首先,我试过: top_row = pdfTable(table_data, colWidths=(3*inch, 3*inch, inch)) 这给了我一个错误: Traceback: File "C:\Python33\lib\site-packages\django\core\ha

我试图从用户指南中自学reportlab。我需要创建的文档只是格式化文本,需要放在页面的特定位置。在以下代码段中,表_数据是一个包含3个字符串的列表。我将表作为pdfTable导入,因为我的应用程序有一个表类

首先,我试过:

top_row = pdfTable(table_data, colWidths=(3*inch, 3*inch, inch))
这给了我一个错误:

Traceback:
File "C:\Python33\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:/Users/Phoenix/PycharmProjects/gamecon\gameconapp\views\utilities.py" in splash_page
  86.                generate_signup_sheets()
File "C:/Users/Phoenix/PycharmProjects/gamecon\gameconapp\views\utilities.py" in generate_signup_sheets
  354.         top_row = pdfTable(table_data, colWidths=(3*inch, 3*inch, inch))
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\tables.py" in __init__
  253.                 raise ValueError("%s data error - %d columns in data but %d in column widths" % (self.identity(),ncols, len(colWidths)))
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\tables.py" in identity
  332.                     v = cv[i][j]

Exception Type: IndexError at /index.html
Exception Value: list index out of range
Traceback:
File "C:\Python33\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:/Users/Phoenix/PycharmProjects/gamecon\gameconapp\views\utilities.py" in splash_page
  86.                generate_signup_sheets()
File "C:/Users/Phoenix/PycharmProjects/gamecon\gameconapp\views\utilities.py" in generate_signup_sheets
  355.         top_row.drawOn(p, 0.75*inch, 0.5*inch)
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\flowables.py" in drawOn
  110.         self._drawOn(canvas)
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\flowables.py" in _drawOn
  91.         self.draw()#this is the bit you overload
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\tables.py" in draw
  1363.         self._drawBkgrnd()
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\tables.py" in _drawBkgrnd
  1386.         colpositions = self._colpositions

Exception Type: AttributeError at /index.html
Exception Value: 'Table' object has no attribute '_colpositions'
因为看起来问题在于列宽,而且文档说Colwidth是可选的,所以我尝试了以下方法:

top_row = pdfTable(table_data)
导致此错误的原因:

Traceback:
File "C:\Python33\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:/Users/Phoenix/PycharmProjects/gamecon\gameconapp\views\utilities.py" in splash_page
  86.                generate_signup_sheets()
File "C:/Users/Phoenix/PycharmProjects/gamecon\gameconapp\views\utilities.py" in generate_signup_sheets
  354.         top_row = pdfTable(table_data, colWidths=(3*inch, 3*inch, inch))
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\tables.py" in __init__
  253.                 raise ValueError("%s data error - %d columns in data but %d in column widths" % (self.identity(),ncols, len(colWidths)))
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\tables.py" in identity
  332.                     v = cv[i][j]

Exception Type: IndexError at /index.html
Exception Value: list index out of range
Traceback:
File "C:\Python33\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:/Users/Phoenix/PycharmProjects/gamecon\gameconapp\views\utilities.py" in splash_page
  86.                generate_signup_sheets()
File "C:/Users/Phoenix/PycharmProjects/gamecon\gameconapp\views\utilities.py" in generate_signup_sheets
  355.         top_row.drawOn(p, 0.75*inch, 0.5*inch)
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\flowables.py" in drawOn
  110.         self._drawOn(canvas)
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\flowables.py" in _drawOn
  91.         self.draw()#this is the bit you overload
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\tables.py" in draw
  1363.         self._drawBkgrnd()
File "C:\Python33\lib\site-packages\reportlab-3.0-py3.3-win-amd64.egg\reportlab\platypus\tables.py" in _drawBkgrnd
  1386.         colpositions = self._colpositions

Exception Type: AttributeError at /index.html
Exception Value: 'Table' object has no attribute '_colpositions'
reportlab文档中的示例均未显示实际使用的表

提前感谢您的帮助

编辑以添加仍然失败的可执行代码:

import os
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.platypus import Table as pdfTable

filename = os.path.join(BASE_DIR, 'pdf_test_%s.pdf' %
                        ( datetime.now().strftime('%d-%b-%Y %H-%M')))

p = canvas.Canvas(filename, pagesize=letter, bottomup=1)
table_data = [['a', 'b', 'c'], ['d', 'e', 'f']]
top_row = pdfTable([table_data])    #, colWidths=(3*inch, 3*inch, inch))
top_row.drawOn(p, 0.75*inch, 0.5*inch)
p.showPage()
p.save()

我认为问题在于你只是传递了一个列表<代码>表格需要列表列表,例如2D数组。从用户指南:

数据参数是每个单元格值的一系列序列 应该使用str函数将其转换为字符串值 或者应该是可流动实例(例如段落)或列表(或 (元组)的实例

若您只有一行,那个么将该行封装到另一个列表中,并将其传递给表。如果这不起作用,请发布一个最小的也是可执行的代码示例

用户指南第77页的p.S.是一个简单的
-类示例

更新 现在我明白了。您使用的是一个
可流动的
,它通常应该通过platypus布局引擎与
DocTemplate
结合使用。第5章对此进行了详细解释。您使用的是手动方式,直接绘制到画布,因此您还必须编写“自己的”布局引擎。在这种情况下,首先必须使用
表的
wrapOn
方法。我真的不知道为什么,但你传递的参数大小似乎并不重要。如果它不起作用,试着利用这些值。这是我调整后的代码版本:

from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.platypus import Table as pdfTable

filename = 'pdf_test.pdf'
p = canvas.Canvas(filename)
table_data = [['a', 'b', 'c'], ['d', 'e', 'f']]
top_row = pdfTable(table_data)
w, h = top_row.wrapOn(p, 0, 0)
top_row.drawOn(p, 0.75*inch, 0.5*inch)
p.showPage()
p.save()

谢谢不知道我怎么漏掉了第77页。不幸的是,将列表包装到一个列表中,或将其设置为列表中的两个列表,所做的只是将“列表索引超出范围”错误更改为“Table”对象没有属性“\u colpositions”错误。所以,至少现在是相同的错误。我将把请求的代码作为编辑放在上面。