逗人,
我一直在创建一个脚本来读取txt并在Plone站点上进行上传(Plone 4.3.10)。 脚本在这里: Script Python using plone.api to create File appear error WrongType when set a file
我使用MrTango描述的提示: Save file to plone site using python
我的难点是将文件附加到在FOR的开头创建的新项目中,具体在以下段落中:
file_obj.file = NamedBlobFile(
data=open(pdf_path, 'r').read(),
contentType='application/pdf',
filename=unicode(file_obj.id, 'utf-8'),
)
参数" data",从文件系统接收文件PDF,但是,不要在新创建的新对象中设置文件。
感谢您的关注!
[UPDATE]
使用pdb,我看到一些输出,数据,contentType n文件名显然设置正确。
那么,我哪里出错了? 或者什么类型的数据输入? 我不是专业的python程序员...就像你看到的...... 如果有人使用plone.api并将pdf上传到plone,你是怎么做到的?
36 pdf_path,
37 filename=unicode(file_obj.id, 'utf-8'),
38 )
39 print('\n \n Criado: '+row['NDOPROCESSO']+'.')
40 transaction.commit()
41
42 pdf_file.close()
43 -> break
44 csvfile.close()
45
(Pdb) file_obj.file.data
'INMEQ/PastaGeral/PROCESSOINMEQ-AL.PDF'
(Pdb) file_obj.file.contentType
'application/pdf'
(Pdb) file_obj.file.filename
u'processoinmeq-al.pdf'
[更新2]
@Mathias,首先,你很棒!
我的O.S.是:
jaf@ocs:~/plone4310/zinstance$ uname -a
Linux ocs 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u6 (2015-11-09) x86_64 GNU/Linux
看看我的尝试:
jaf@ocs:~/plone4310/zinstance$ bin/instance -O a debug
Starting debugger (the name "app" is bound to the top-level Zope object)
>>> plone = app.a
>>> plone
<PloneSite at /a>
>>> from zope.component.hooks import setSite
>>> setSite(plone)
>>> from plone import api
>>> pdfpath = 'INMEQ/PastaGeral/PROCESSOINMEQ-AL.PDF'
#I tried too with a full path, like you see in image print below
#'/home/jaf/plone4310/zinstance/INMEQ/PastaGeral/PROCESSOINMEQ-AL.PDF'
>>> pdfpath
'INMEQ/PastaGeral/PROCESSOINMEQ-AL.PDF'
>>> obj = api.content.create(type='File', title='a file', container=plone)
>>> obj
<ATFile at /a/a-file> #unique difference, between you and me, is ATFile not File.
>>> obj.id
'a-file'
>>> file_ = plone.get(obj.id)
>>> file_
<ATFile at /a/a-file>
>>> file_ = plone.get('a-file')
>>> file_
<ATFile at /a/a-file>
>>> from plone.namedfile.file import NamedFile
>>> pdf = open(pdfpath, 'r')
>>> pdf
<open file 'INMEQ/PastaGeral/PROCESSOINMEQ-AL.PDF', mode 'r' at 0x7f41b7b48030>
>>> file_.file = NamedFile(data=pdf, filename=unicode(obj.id, 'utf-8'), contentType='application/pdf')
>>> file_.file
<plone.namedfile.file.NamedFile object at 0x7f41b7b42500>
>>> import transaction
>>> transaction.commit()
>>> file_.file
<plone.namedfile.file.NamedFile object at 0x7f41b7b42500>
>>>
在transaction.commit()之后,我们拥有它。
[更新3 - 及工作]
使用@Mathias的SUPER HELP,脚本python从txt上传File
Archetypes并用分号分隔。
from zope.site.hooks import setSite
from plone import api
import transaction
import csv
import os
local_path = '/path_to_plone_instace/plone4310/zinstance'
scan_path = 'path_with_txt_and_PDFs'
geral_path = 'folder_with_pdf_only'
txt_name = 'file_with_content.txt'
plone_site = 'plone_site'
plone_site_pasta = 'folder_upload_in_plone'
portal = app[plone_site]
setSite(portal)
container = portal[plone_site_pasta]
with open(os.path.join(local_path, scan_path, txt_name), 'rb') as csvfile:
reader = csv.DictReader(csvfile, delimiter=';', quotechar='|')
for row in reader:
pdf_id = str(row['PDF_ID'])
pdf_file = open(os.path.join(local_path, scan_path, geral_path, str(pdf_id)), 'r')
file_obj = api.content.create(
container=container,
type='File',
title=str(row['PDF_TITLE']),
description=str(row['PDF_DESCRIPTION']),
safe_id=True,
excludeFromNav=True
)
file_ = container.get(file_obj.id)
file_.setFile(pdf_file)
if int(file_obj.getFile().size()) <= 0:
print str(file_obj.id) + ', Empty FILE!!! size: ' + str(file_obj.getFile().size())
else:
print str(file_obj.title) + ', success created, with size: ' + str(file_obj.getFile().size())
transaction.commit()
csvfile.close()
答案 0 :(得分:4)
以下是Plone 4.3.10 + plone.app.contenttypes(默认DX类型)
>>> plone = app.Plonedemo
>>> plone
<PloneSite at /Plonedemo>
>>> from zope.component.hooks import setSite
>>> setSite(plone)
>>> from plone import api
>>> pdfpath = '/Users/PATHTOPDF'
>>> obj = api.content.create(
... type='File',
... title='a file',
... container=plone)
>>> file_ = plone.get('a-file')
>>> file_
<File at /Plonedemo/a-file>
我使用NameFile而不是NamedBlobFile .. AFAIK都做同样的事情。
>>> from plone.namedfile.file import NamedFile
看起来您需要提供文件处理程序而不是实际内容。
>>> pdf = open(pdfpath, 'r')
>>> pdf
<open file '/Users/PATHTOPDF', mode 'r' at 0x10dca3150>
>>> file_.file = NamedFile(data=pdf, filename=u'bla.pdf', contentType='application/pdf')
>>> import transaction
>>> transaction.commit()
>>> file_.file
<plone.namedfile.file.NamedFile object at 0x10dca0230>
启动实例后,文件包含。 PDF在那里,工作正常。
更新:原型File
内容的示例。
现在必须这样做: - )
我假设你有一个原型文件内容 您可以按照相同的步骤将文件添加到内容中。
...
>>> pdf = open(pdfpath, 'r')
>>> pdf
<open file '/Users/PATHTOPDF', mode 'r' at 0x10dca3150>
>>> file_.setFile(pdf) # filehandler, NOT THE NAMEDFILE.
>>> obj.getFile().size()
155847
>>> import transaction
>>> transaction.commit()
此示例使用Archetypes默认的setter / getter。
答案 1 :(得分:2)
PDF是一种二进制格式,因此您可能希望了解如何在Python中打开文件。像data=open(pdf_path, 'rb')
这样的东西应该会有所帮助。
另外,我建议您了解pdb并在此行之前插入一个断点,以便检查一切是否按预期工作。