V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
albertofwb
V2EX  ›  外包

使用 Python 提取 13 中 office 文件内嵌图片,预算 2-3k

  •  
  •   albertofwb · 341 天前 · 811 次点击
    这是一个创建于 341 天前的主题,其中的信息可能已经有所发展或是发生改变。

    vx: do-you-enjoy-coding

    项目地址: https://github.com/albertofwb/embed_files

    合作方式:fork 后在自己的 repo 开发,然后我会将项目改为 private 需要你将自己的项目也改为 private 然后给我读写权限

    报价: 2000-3000 工期: 7 天

    请合理评估本项目难度,总共 13 种文件类型,如能力有限,只能提供部分格式的支持,按照百分比支付佣金

    dream10201
        1
    dream10201  
       341 天前
    前两天才写了个从 excel 中提取 pdf
    albertofwb
        2
    albertofwb  
    OP
       341 天前
    @dream10201 要不要试试
    dream10201
        3
    dream10201  
       341 天前
    @albertofwb 不用了,兴趣不大,谢谢!
    nolog
        4
    nolog  
       341 天前
    为什么会有依赖大小限制?
    albertofwb
        5
    albertofwb  
    OP
       341 天前
    @nolog 这只是本体软件一个很小的部分,太大的话,影响总体加载速度
    Alias4ck
        6
    Alias4ck  
       341 天前   ❤️ 1
    纯属好奇, 看了一下 OLE 格式 从中二进制流数据解析图片出来挺麻烦的

    虽然市面上有几种库,但是基本没这个功能,大部分是打包 zip,去找后缀名,这种方式对 ppt 可以,word 这种就不生效了( https://github.com/surajForDD/AttachmentsExtractor/)

    也有用 C 写( https://github.com/inflex/ripOLE)的,但是他做的也不完整只写了 png signature 的
    其他类型的没有

    python 也有相关的,但库很久没人维护了 https://github.com/decalage2/oletools
    解析图片也有人提过相关的 issue( https://github.com/decalage2/oletools/issues/457)

    总的来说,流程应该挺复杂,而且你给的文件格式有的不是 Composite Document File V2 Document.
    Alias4ck
        7
    Alias4ck  
       341 天前
    简单的写了个 demo,应该可以
    albertofwb
        8
    albertofwb  
    OP
       341 天前
    oletools 这个我在 python 里用过,效果不理想。
    你截图中的成功我使用 re 匹配 signature 也能做到
    ```python
    def fetch_img_regex(buf: bytes):
    # ref https://chujian521.github.io/blog/2018/07/31/%E5%B8%B8%E8%A7%81%E5%9B%BE%E7%89%87%E6%A0%BC%E5%BC%8F%E5%88%86%E6%9E%90%E6%80%BB%E7%BB%93/
    pattern_map = {
    b'\xff\xd8\xff\xe0.*\xff\xd9': 'jpg',
    b'\x89\x50\x4e\x47\x0D\x0A\x1A\x0A.*\x00\x00\x00\x00\x49\x45\x4E\x44....': 'png'
    }
    # FIXME: the re pattern can only find one matched items
    # then there has more than one images it merge them into single one and the file can not being parsed
    for pattern, suffix in pattern_map.items():
    data = re.findall(pattern, buf, re.DOTALL)
    if len(data) > 0:
    return data, suffix
    raise NoImgContainedException()
    ```
    https://imgur.com/a/QX51prW

    @Alias4ck
    Alias4ck
        9
    Alias4ck  
       341 天前   ❤️ 1
    @albertofwb 基本原理应该就是这样从 binary stream 里面匹配 signature 了,大差不差,用 re 去匹配只是其中实现的一种方式, 如果觉得 python olefile 库写的不好,用 rust/c 实现吧 毕竟 ole file 结构摆在那里了

    🫤随便做了个 research
    https://gist.github.com/noahlias/9c90aab66e322977b4a02fa17cb6a809
    albertofwb
        10
    albertofwb  
    OP
       320 天前
    该外包已经完成
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2721 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 06:14 · PVG 14:14 · LAX 23:14 · JFK 02:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.