在 plone 上获取上传的文件并通过 python 脚本下载它们?
Take uploaded files on plone and download them via a python script?
我在 plone 上创建了一个文档站点,可以从中上传文件。我看到 plone 以 blob 的形式将它们保存在文件系统中,现在我需要通过一个 python 脚本来处理它们,该脚本将处理使用 OCR 下载的 pdf。有谁知道该怎么做?谢谢
不确定如何从 BLOB 存储中提取 PDF 或者是否可能,但是您可以从 运行 克隆站点中提取它们(例如通过浏览器视图执行脚本):
import os
from Products.CMFCore.utils import getToolByName
def isPdf(search_result):
"""Check mime_type for Plone >= 5.1, otherwise check file-extension."""
if mimeTypeIsPdf(search_result) or search_result.id.endswith('.pdf'):
return True
return False
def mimeTypeIsPdf(search_result):
"""
Plone-5.1 introduced the mime_type-attribute on files.
Try to get it, if it doesn't exist, fail silently.
Return True if mime_type exists and is PDF, otherwise False.
"""
try:
mime_type = search_result.mime_type
if mime_type == 'application/pdf':
return True
except:
pass
return False
def exportPdfFiles(context, export_path):
"""
Get all PDF-files of site and write them to export_path on the filessytem.
Remain folder-structure of site.
"""
catalog = getToolByName(context, 'portal_catalog')
search_results = catalog(portal_type='File', Language='all')
for search_result in search_results:
# For each PDF-file:
if isPdf(search_result):
file_path = export_path + search_result.getPath()
file_content = search_result.getObject().data
parent_path = '/'.join(file_path.split('/')[:-1])
# Create missing directories on the fly:
if not os.path.exists(parent_path):
os.makedirs(parent_path)
# Write PDF:
with open(file_path, 'w') as fil:
fil.write(file_content)
print 'Wrote ' + file_path
print 'Finished exporting PDF-files to ' + export_path
该示例将 Plone 站点的文件夹结构保留在导出目录中。如果您希望它们平放在一个目录中,则需要一个用于重复文件名的处理程序。
我在 plone 上创建了一个文档站点,可以从中上传文件。我看到 plone 以 blob 的形式将它们保存在文件系统中,现在我需要通过一个 python 脚本来处理它们,该脚本将处理使用 OCR 下载的 pdf。有谁知道该怎么做?谢谢
不确定如何从 BLOB 存储中提取 PDF 或者是否可能,但是您可以从 运行 克隆站点中提取它们(例如通过浏览器视图执行脚本):
import os
from Products.CMFCore.utils import getToolByName
def isPdf(search_result):
"""Check mime_type for Plone >= 5.1, otherwise check file-extension."""
if mimeTypeIsPdf(search_result) or search_result.id.endswith('.pdf'):
return True
return False
def mimeTypeIsPdf(search_result):
"""
Plone-5.1 introduced the mime_type-attribute on files.
Try to get it, if it doesn't exist, fail silently.
Return True if mime_type exists and is PDF, otherwise False.
"""
try:
mime_type = search_result.mime_type
if mime_type == 'application/pdf':
return True
except:
pass
return False
def exportPdfFiles(context, export_path):
"""
Get all PDF-files of site and write them to export_path on the filessytem.
Remain folder-structure of site.
"""
catalog = getToolByName(context, 'portal_catalog')
search_results = catalog(portal_type='File', Language='all')
for search_result in search_results:
# For each PDF-file:
if isPdf(search_result):
file_path = export_path + search_result.getPath()
file_content = search_result.getObject().data
parent_path = '/'.join(file_path.split('/')[:-1])
# Create missing directories on the fly:
if not os.path.exists(parent_path):
os.makedirs(parent_path)
# Write PDF:
with open(file_path, 'w') as fil:
fil.write(file_content)
print 'Wrote ' + file_path
print 'Finished exporting PDF-files to ' + export_path
该示例将 Plone 站点的文件夹结构保留在导出目录中。如果您希望它们平放在一个目录中,则需要一个用于重复文件名的处理程序。