将上传的文件放在plone上并通过python脚本下载?

时间:2019-04-11 09:23:26

标签: python pdf blob ocr plone

我在plone上创建了一个文档站点,可以从该站点上载文件。我看到plone以blob的形式将它们保存在文件系统中,现在我需要通过python脚本来处理它们,该脚本将处理通过OCR下载的pdf。有谁知道如何做吗?谢谢

1 个答案:

答案 0 :(得分:2)

不确定如何从BLOB存储中提取PDF或完全可能,但是您可以从运行的Plone站点中提取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站点的文件夹结构保留在export-directory中。如果要将它们平放在一个目录中,则需要一个用于重复文件名的处理程序。