使用plone.api创建文件的脚本Python在设置文件时出现错误WrongType [英] Script Python using plone.api to create File appear error WrongType when set a file

查看:127
本文介绍了使用plone.api创建文件的脚本Python在设置文件时出现错误WrongType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的

我正在创建一个脚本python以在Plone站点中批量上传文件,安装是UnifiedInstaller Plone 4.3.10.

I'm creating a script python to mass upload files in Plone site, the installation is UnifiedInstaller Plone 4.3.10.

此脚本读取txt,并且该txt与分号分隔,在新创建的项目中设置文件时出现错误.

This script read a txt, and this txt have separation with semicolon, the error appear when set up a file in a new created item.

遵循脚本.

from zope.site.hooks import setSite
from plone.namedfile.file import NamedBlobFile
from plone import api
import transaction
import csv

portal = app['Plone']
setSite(portal)
container = portal['PROCESSOS']

with open('CARGA/C008_0002.txt', 'rb') as csvfile:
    reader = csv.DictReader(csvfile, delimiter=';', quotechar='|')
    for row in reader:
        pdf_id = 'P'+str(row['IMAGEM']).strip('Pasta Geral\\ ')
        file_obj = api.content.create(                          
            container, 'File',
            title=str(row['INTERESSADO']),
            id=pdf_id,
            description=str(row['CNPJ / CPF'])+' '+str(row['ASSUNTO']),
            safe_id=True
        )
        pdf_path = 'INMEQ/'+str(row['IMAGEM']).replace("\\", "/")
        print(pdf_path)
        file_obj.file = NamedBlobFile(
            data=open(pdf_path, 'r').read(),
            contentType='application/pdf',
            filename=str(file_obj.id),
        )
        print('Created: '+row['NDOPROCESSO']+'.')

transaction.commit()

当脚本将设置文件时,将出现错误"WrongType".参见详细的波纹管.

When the script will set up a file the error "WrongType" appear. See verbose bellow.

Traceback (most recent call last):
  File "<console>", line 18, in <module>
  File "/home/jaf/plone4310/buildout-cache/eggs/plone.namedfile-3.0.9-py2.7.egg/plone/namedfile/file.py", line 384, in __init__
    self.filename = filename
  File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/fieldproperty.py", line 52, in __set__
    field.validate(value)
  File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 182, in validate
    self._validate(value)
  File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 309, in _validate
    super(MinMaxLen, self)._validate(value)
  File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 209, in _validate
    raise WrongType(value, self._type, self.__name__)
WrongType: ('processo-al-1.pdf', <type 'unicode'>, 'filename')

感谢您的关注!

- 朱利诺·阿劳霍

推荐答案

您需要将文件名作为unicode传递.

You need to pass the filename as unicode.

file_obj.file = NamedBlobFile(
    data=open(pdf_path, 'r').read(),
    contentType='application/pdf',
    filename=unicode(file_obj.id),  # needs to be unicode
)

plone.namedfile文档中的更多信息-> https://github.com/plone/plone.namedfile/blob/36014d67c3befacfe3a058f1d3d99a6a4352a31f/plone/namedfile/usage.rst

More Info in the plone.namedfile docu --> https://github.com/plone/plone.namedfile/blob/36014d67c3befacfe3a058f1d3d99a6a4352a31f/plone/namedfile/usage.rst

这篇关于使用plone.api创建文件的脚本Python在设置文件时出现错误WrongType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆