推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
renoyuan
V2EX  ›  Python

使用 easyofd 解析 ofd 文件

  •  
  •   renoyuan · Jan 23, 2024 · 4685 views
    This topic created in 977 days ago, the information mentioned may be changed or developed.

    使用 easyofd 解析 ofd 文件

    关于 OFD 格式

    OFD 格式简单来说是 PDF 的国产替代。目前只有国内一定范围内在用,所以相对应的工具库还比较少。

    安装 easyofd

    pip 安装

    pip install easyofd
    

    github 源码地址

    git clone https://github.com/renoyuan/easyofd.git

    使用 easyofd

    ofd 转 pfd

    import base64
    import json
    from PIL import Image
    
    from easyofd.ofd import OFD
    
    with open(r"增值税电子专票 5.ofd","rb") as f:
        ofdb64 = str(base64.b64encode(f.read()),"utf-8")
        ofd = OFD() # 初始化 OFD 工具类
        ofd.read(ofdb64,save_xml=False, xml_name="testxml") # 读取 ofdb64
        # print(ofd.data) # ofd.data 为程序解析结果
        pdf_bytes = ofd.to_pdf() # 转 pdf
       
        ofd.del_data()
        
        with open(r"增值税电子专票 5.pdf","wb") as f:
            f.write(pdf_bytes)
    

    ofd 转图片

    import base64
    import json
    from PIL import Image
    
    from easyofd.ofd import OFD
    
    
    with open(r"增值税电子专票 5.ofd","rb") as f:
            ofdb64 = str(base64.b64encode(f.read()),"utf-8")
        ofd = OFD() # 初始化 OFD 工具类
        ofd.read(ofdb64,save_xml=False, xml_name="testxml") # 读取 ofdb64
        # print(ofd.data) # ofd.data 为程序解析结果
        img_np = ofd.to_jpg() # 转图片
        ofd.del_data()
        for idx, img in enumerate(img_np):
            im = Image.fromarray(img)
            im.save(f"test_img{idx}.jpg")
    

    客户端

    easyofd 有一个 windows 客户端 程序可以不需要代码直接使用

    下载地址: https://github.com/renoyuan/easyofd/releases

    2 replies  •  2024-12-25 15:33:23 +08:00
    onesec
        1
    onesec  
       Jan 24, 2024
    mark~
    电子发票现在都用 ofd 格式吗?我看了一下滴滴之类的发票还是 pdf
    renoyuan
        2
    renoyuan  
    OP
       Dec 25, 2024
    对个人和私企还主要是 pdf 把。目前主要应用还是国企和机关单位,需要用国产化的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1232 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:23 · PVG 01:23 · LAX 10:23 · JFK 13:23
    ♥ Do have faith in what you're doing.