hzexcel


Namehzexcel JSON
Version 0.0.13 PyPI version JSON
download
home_page
Summary提供常用django、Python后端的excel导入导出功能
upload_time2023-08-09 11:11:46
maintainer
docs_urlNone
author
requires_python>=3.7
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## **1概述**

# 1.1可根据配置、参数提供校验和导入导出excel功能

# 1.2贡献者:深圳市汉卓软件

# 2.功能详细说明(API列表):
# (1)export_excel(data, data_type{可选},file_name{可选} , rules{可选}, freeze{可选})
**导出excel** 。	   返回参数:成功时:返回excel 文件;失败时状态码为非200,返回错误信息。

# (2)import_excel(file, check_dict{可选},type_dict{可选})
**导入excel并根据规则校验**。返回参数:成功时:返回{"status":200,"msg":"校验成功!","data":[{"Name":"Alice","Age":25,"City":"New York"},{"Name":"Bob","Age":32,"City":"Los Angeles"},{"Name":"Charlie","Age":22,"City":"London"}]};失败时,返回的状态码为非200,错误信息例子如下:"缺少列名: 'City';<br>"


### 2.应用说明:
#### (1)export_excel参数说明
data:必选数据,有两种不同的数据结构可选,一种是每一行是一条数据,如:{'测试':['Alice','Bob','Charlie'],'Name':['Alice','Bob','Charlie'],'Age':[25,32,22],'City':['New York','Los Angeles','London']}
另一种是类似 DataFrame 的数据格式,比如:data:{"data":[['Alice',25,'New York'],['Bob',32,'Los Angeles'],['Charlie',22,'London']],"columns":['Name','Age','City']}

data_type: 可选参数,默认为1。可选值为 1 或 2。当值为 1 时,表示每一行是一条数据;当值为 2 时,表示使用 DataFrame 类型的数据格式。
file_name: 可选参数,文件名称,比如 '默认名称.xlsx'。

rules: 可选参数,导出规则,包括表头设置和数据验证下拉列表等。

freeze:可选参数,表示从第几行第几列开始冻结,比如值为”A2 ”其中2表示第从第2行(行索引从1开始), A 表示第一列。

#### (2)import_excel参数说明

file :必选参数,二进制excel文件,一般从前端传来的 self.request.FILES['file'] 中获取
check_dict:可选参数,校验的对象 ,其中header表示必须必须有那些列,content表示那些列 必填、而且指定类型,

type_dict:可选参数,
判断非表头的数据类型,可传入配置设置可校验类型,不传时默认值如下{
            "str": str,
            "int": int,
            "float": float,
            "bool": bool,
            "datetime": pd.to_datetime,
        }

## 3.应用实例
### (1)export_excel例子:
from hzexcel import export_excel
rules = {
    "head": {"Font": {"bold": True, "color": "FF0000", "size": 14}},
    "validation": [
        {
            "column_name": "Name",
            "type": "list",
            "formula1": '"$ 价格,A 可获得性,P 包装,P 性能,E 易用性,A 保证,L 生命周期成本,S 社会影响力"',
            "showDropDown": False,
        }
    ],
}
data = {
    '测试': ['Alice', 'Bob', 'Charlie'],
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 32, 22],
    'City': ['New York', 'Los Angeles', 'London']
}
return export_excel(data, 2, '默认名称.xlsx', rules, "A2")


## (2)export_excel例子:
from hzexcel import import_excel
self.request = request
file = self.request.FILES['file']
check_dict = {
    "header": ['Name', 'Age', 'City'],
    "content": {'Name': 'str', 'City': 'str'}
}

rs0 = import_excel(file, check_dict)
rs = json.loads(rs0)
if rs['status']:
    #  通过验证
    # 将文件中的数据转为数据 ,批量插入到数据库
    if rs['status'] == 200:
        return Response(rs, status=status.HTTP_200_OK)
    else:
        return Response(f"{rs['msg']}", status=status.HTTP_400_BAD_REQUEST)
else:
    return Response(f"校验数据时出错", status=status.HTTP_400_BAD_REQUEST)

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "hzexcel",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "myliao Nathen <1739951529@qq.com>",
    "download_url": "https://files.pythonhosted.org/packages/61/37/6159057b9a28477dc07c4402d88f61941f054324542a2fc79684c2d972fb/hzexcel-0.0.13.tar.gz",
    "platform": null,
    "description": "## **1\u6982\u8ff0**\n\n# 1.1\u53ef\u6839\u636e\u914d\u7f6e\u3001\u53c2\u6570\u63d0\u4f9b\u6821\u9a8c\u548c\u5bfc\u5165\u5bfc\u51faexcel\u529f\u80fd\n\n# 1.2\u8d21\u732e\u8005\uff1a\u6df1\u5733\u5e02\u6c49\u5353\u8f6f\u4ef6\n\n# 2.\u529f\u80fd\u8be6\u7ec6\u8bf4\u660e(API\u5217\u8868)\uff1a\n# (1)export_excel(data, data_type{\u53ef\u9009},file_name{\u53ef\u9009} , rules{\u53ef\u9009}, freeze{\u53ef\u9009})\n**\u5bfc\u51faexcel** \u3002\t   \u8fd4\u56de\u53c2\u6570\uff1a\u6210\u529f\u65f6\uff1a\u8fd4\u56deexcel \u6587\u4ef6\uff1b\u5931\u8d25\u65f6\u72b6\u6001\u7801\u4e3a\u975e200\uff0c\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u3002\n\n# (2)import_excel(file, check_dict{\u53ef\u9009},type_dict{\u53ef\u9009})\n**\u5bfc\u5165excel\u5e76\u6839\u636e\u89c4\u5219\u6821\u9a8c**\u3002\u8fd4\u56de\u53c2\u6570\uff1a\u6210\u529f\u65f6\uff1a\u8fd4\u56de{\"status\":200,\"msg\":\"\u6821\u9a8c\u6210\u529f\uff01\",\"data\":[{\"Name\":\"Alice\",\"Age\":25,\"City\":\"New York\"},{\"Name\":\"Bob\",\"Age\":32,\"City\":\"Los Angeles\"},{\"Name\":\"Charlie\",\"Age\":22,\"City\":\"London\"}]}\uff1b\u5931\u8d25\u65f6\uff0c\u8fd4\u56de\u7684\u72b6\u6001\u7801\u4e3a\u975e200\uff0c\u9519\u8bef\u4fe1\u606f\u4f8b\u5b50\u5982\u4e0b\uff1a\"\u7f3a\u5c11\u5217\u540d\uff1a 'City';<br>\"\n\n\n### 2.\u5e94\u7528\u8bf4\u660e\uff1a\n#### (1)export_excel\u53c2\u6570\u8bf4\u660e\ndata:\u5fc5\u9009\u6570\u636e\uff0c\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u6570\u636e\u7ed3\u6784\u53ef\u9009\uff0c\u4e00\u79cd\u662f\u6bcf\u4e00\u884c\u662f\u4e00\u6761\u6570\u636e\uff0c\u5982\uff1a{'\u6d4b\u8bd5':['Alice','Bob','Charlie'],'Name':['Alice','Bob','Charlie'],'Age':[25,32,22],'City':['New York','Los Angeles','London']}\n\u53e6\u4e00\u79cd\u662f\u7c7b\u4f3c DataFrame \u7684\u6570\u636e\u683c\u5f0f\uff0c\u6bd4\u5982\uff1adata:{\"data\":[['Alice',25,'New York'],['Bob',32,'Los Angeles'],['Charlie',22,'London']],\"columns\":['Name','Age','City']}\n\ndata_type: \u53ef\u9009\u53c2\u6570\uff0c\u9ed8\u8ba4\u4e3a1\u3002\u53ef\u9009\u503c\u4e3a 1 \u6216 2\u3002\u5f53\u503c\u4e3a 1 \u65f6\uff0c\u8868\u793a\u6bcf\u4e00\u884c\u662f\u4e00\u6761\u6570\u636e\uff1b\u5f53\u503c\u4e3a 2 \u65f6\uff0c\u8868\u793a\u4f7f\u7528 DataFrame \u7c7b\u578b\u7684\u6570\u636e\u683c\u5f0f\u3002\nfile_name: \u53ef\u9009\u53c2\u6570\uff0c\u6587\u4ef6\u540d\u79f0\uff0c\u6bd4\u5982 '\u9ed8\u8ba4\u540d\u79f0.xlsx'\u3002\n\nrules: \u53ef\u9009\u53c2\u6570\uff0c\u5bfc\u51fa\u89c4\u5219\uff0c\u5305\u62ec\u8868\u5934\u8bbe\u7f6e\u548c\u6570\u636e\u9a8c\u8bc1\u4e0b\u62c9\u5217\u8868\u7b49\u3002\n\nfreeze\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u8868\u793a\u4ece\u7b2c\u51e0\u884c\u7b2c\u51e0\u5217\u5f00\u59cb\u51bb\u7ed3\uff0c\u6bd4\u5982\u503c\u4e3a\u201dA2 \u201d\u5176\u4e2d2\u8868\u793a\u7b2c\u4ece\u7b2c2\u884c\uff08\u884c\u7d22\u5f15\u4ece1\u5f00\u59cb\uff09\uff0c A \u8868\u793a\u7b2c\u4e00\u5217\u3002\n\n#### (2)import_excel\u53c2\u6570\u8bf4\u660e\n\nfile :\u5fc5\u9009\u53c2\u6570\uff0c\u4e8c\u8fdb\u5236excel\u6587\u4ef6\uff0c\u4e00\u822c\u4ece\u524d\u7aef\u4f20\u6765\u7684 self.request.FILES['file'] \u4e2d\u83b7\u53d6\ncheck_dict:\u53ef\u9009\u53c2\u6570\uff0c\u6821\u9a8c\u7684\u5bf9\u8c61 \uff0c\u5176\u4e2dheader\u8868\u793a\u5fc5\u987b\u5fc5\u987b\u6709\u90a3\u4e9b\u5217\uff0ccontent\u8868\u793a\u90a3\u4e9b\u5217 \u5fc5\u586b\u3001\u800c\u4e14\u6307\u5b9a\u7c7b\u578b\uff0c\n\ntype_dict:\u53ef\u9009\u53c2\u6570\uff0c\n\u5224\u65ad\u975e\u8868\u5934\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4f20\u5165\u914d\u7f6e\u8bbe\u7f6e\u53ef\u6821\u9a8c\u7c7b\u578b\uff0c\u4e0d\u4f20\u65f6\u9ed8\u8ba4\u503c\u5982\u4e0b{\n            \"str\": str,\n            \"int\": int,\n            \"float\": float,\n            \"bool\": bool,\n            \"datetime\": pd.to_datetime,\n        }\n\n## 3.\u5e94\u7528\u5b9e\u4f8b\n### \uff081\uff09export_excel\u4f8b\u5b50\uff1a\nfrom hzexcel import export_excel\nrules = {\n    \"head\": {\"Font\": {\"bold\": True, \"color\": \"FF0000\", \"size\": 14}},\n    \"validation\": [\n        {\n            \"column_name\": \"Name\",\n            \"type\": \"list\",\n            \"formula1\": '\"$ \u4ef7\u683c,A \u53ef\u83b7\u5f97\u6027,P \u5305\u88c5,P \u6027\u80fd,E \u6613\u7528\u6027,A \u4fdd\u8bc1,L \u751f\u547d\u5468\u671f\u6210\u672c,S \u793e\u4f1a\u5f71\u54cd\u529b\"',\n            \"showDropDown\": False,\n        }\n    ],\n}\ndata = {\n    '\u6d4b\u8bd5': ['Alice', 'Bob', 'Charlie'],\n    'Name': ['Alice', 'Bob', 'Charlie'],\n    'Age': [25, 32, 22],\n    'City': ['New York', 'Los Angeles', 'London']\n}\nreturn export_excel(data, 2, '\u9ed8\u8ba4\u540d\u79f0.xlsx', rules, \"A2\")\n\n\n## \uff082\uff09export_excel\u4f8b\u5b50\uff1a\nfrom hzexcel import import_excel\nself.request = request\nfile = self.request.FILES['file']\ncheck_dict = {\n    \"header\": ['Name', 'Age', 'City'],\n    \"content\": {'Name': 'str', 'City': 'str'}\n}\n\nrs0 = import_excel(file, check_dict)\nrs = json.loads(rs0)\nif rs['status']:\n    #  \u901a\u8fc7\u9a8c\u8bc1\n    # \u5c06\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u8f6c\u4e3a\u6570\u636e \uff0c\u6279\u91cf\u63d2\u5165\u5230\u6570\u636e\u5e93\n    if rs['status'] == 200:\n        return Response(rs, status=status.HTTP_200_OK)\n    else:\n        return Response(f\"{rs['msg']}\", status=status.HTTP_400_BAD_REQUEST)\nelse:\n    return Response(f\"\u6821\u9a8c\u6570\u636e\u65f6\u51fa\u9519\", status=status.HTTP_400_BAD_REQUEST)\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "\u63d0\u4f9b\u5e38\u7528django\u3001Python\u540e\u7aef\u7684excel\u5bfc\u5165\u5bfc\u51fa\u529f\u80fd",
    "version": "0.0.13",
    "project_urls": {
        "Bug Tracker": "https://github.com/pypa/sampleproject/issues",
        "Homepage": "https://github.com/pypa/sampleproject"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9ca85d3a88fa3db2d3bfab3353c08595cfbfc5698fdc2412a360a95aa70d30ef",
                "md5": "2e4e920166f838f22cc5e7f8d13171fa",
                "sha256": "010fcd106333019a353545f3b70a4da27f040abb64fed6d008dd2bb5e28488dc"
            },
            "downloads": -1,
            "filename": "hzexcel-0.0.13-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2e4e920166f838f22cc5e7f8d13171fa",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 10040,
            "upload_time": "2023-08-09T11:11:45",
            "upload_time_iso_8601": "2023-08-09T11:11:45.107576Z",
            "url": "https://files.pythonhosted.org/packages/9c/a8/5d3a88fa3db2d3bfab3353c08595cfbfc5698fdc2412a360a95aa70d30ef/hzexcel-0.0.13-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "61376159057b9a28477dc07c4402d88f61941f054324542a2fc79684c2d972fb",
                "md5": "ceeff303afb835133ed8902a6773bd48",
                "sha256": "9f9eb23ebdf597aca2893e991012dfbe69ce99ebc690e51cb24716c9e0a9113f"
            },
            "downloads": -1,
            "filename": "hzexcel-0.0.13.tar.gz",
            "has_sig": false,
            "md5_digest": "ceeff303afb835133ed8902a6773bd48",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 9485,
            "upload_time": "2023-08-09T11:11:46",
            "upload_time_iso_8601": "2023-08-09T11:11:46.860023Z",
            "url": "https://files.pythonhosted.org/packages/61/37/6159057b9a28477dc07c4402d88f61941f054324542a2fc79684c2d972fb/hzexcel-0.0.13.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-09 11:11:46",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pypa",
    "github_project": "sampleproject",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "hzexcel"
}
        
Elapsed time: 0.19143s