2023年12月8日发(作者:)

Python解析ofd文件

ofd文件解析步骤

1、先

import zipfile

解压ofd文件,获得xml文件,然后在浏览器打开xml文件即可看到内容

2、再通过

import xmltodict

解析xml文件

3、完整代码如下

# encoding: utf-8

import zipfile

import xmltodict

import requests

import os

import shutil

def unzip_file(zip_path, unzip_path=None):

"""

:param zip_path: ofd格式文件路径

:param unzip_path: 解压后的文件存放目录

:return: unzip_path

"""

if not unzip_path:

unzip_path = zip_('.')[0]

with e(zip_path, 'r') as f:

for file in st():

t(file, path=unzip_path)

return unzip_path

def parse_ofd(content, path):

"""

:param content: ofd文件字节内容

:param path: ofd文件存取路径

"""

with open(path, "wb") as f:

(content)

file_path = unzip_file(path)

xml_path = f"{file_path}/"

data_dict = {}

with open(xml_path, "r", encoding="utf-8") as f:

_text = ()

tree = (_text)

#

以下解析部分

for row in tree['ofd:OFD']['ofd:DocBody']['ofd:DocInfo']['ofd:CustomDatas']['ofd:CustomData']:

data_dict[row['@Name']] = ('#text')

(file_path)

(path)

return data_dict

headers = {

'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.

9',9', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',}ofd_url = "222.143.33.74/spyx_api/storage/upload/qianzhang/"res = (ofd_url, headers=headers)_data_dict = parse_ofd(t, f'E://{ofd_("/")[-1]}')print(_data_dict)