python - 在plone上上传文件并通过python脚本下载它们?
问题描述
我在 plone 上创建了一个文档站点,可以从中进行文件上传。我看到 plone 以 blob 的形式将它们保存在文件系统中,现在我需要通过一个 python 脚本来处理它们,该脚本将处理使用 OCR 下载的 pdf。有谁知道该怎么做?谢谢
解决方案
不确定如何从 BLOB-storage 中提取 PDF,或者是否有可能,但您可以从正在运行的 Plone 站点中提取它们(例如,通过浏览器视图执行脚本):
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 站点的文件夹结构保留在导出目录中。如果您希望它们平放在一个目录中,则需要重复文件名的处理程序。
推荐阅读
- python - 如何从输出中删除索引
- angular - Ionic 4 离子选择选项值变化
- r - 如何检查数据框的逐行条件(使用 dplyr、purrr 等)?
- kotlin - 惯用的 Kotlin 方法从标准输入求和数字直到满足条件
- yaml - Ceedling 具有多个源文件扩展名
- c# - System.ArgumentNullException: '值不能为空。参数名称:connectionString'
- groovy - 如何编写for循环以使用不同的参数多次执行jar任务?
- angular - 我想画一条水平线
- r - Head() 函数未在 R markdown 中显示输出
- python - 组合两个相互依赖的数据集的最佳方法是什么?