{ "cells": [ { "cell_type": "markdown", "id": "7b8c910e-dc62-4710-a40e-044e8d9b32ca", "metadata": {}, "source": [ "## 获取科目表信息" ] }, { "cell_type": "code", "execution_count": null, "id": "23a33174-3522-4a4e-b0ec-fde8ab0e6277", "metadata": {}, "outputs": [], "source": [ "import pymysql\n", "db = pymysql.connect(host = \"172.19.0.12\",user = \"songyi\",password = \"yylzs\",database = \"_5e5899d8398b5f7b\" )\n", "cursor = db.cursor()\n", "sql = 'select account_name,account_number,parent_account from tabAccount where company= %s'\n", "cursor.execute(sql, {'北京运动一百体育文化发展有限公司'})\n", "data = cursor.fetchall()\n", "for item in data:\n", " print(item)\n", "print(type(data))" ] }, { "cell_type": "code", "execution_count": null, "id": "b91b590d-3761-4da9-842c-2109d63a8279", "metadata": {}, "outputs": [], "source": [ "import pymysql\n", "db = pymysql.connect(host = \"172.19.0.12\",user = \"songyi\",password = \"yylzs\",database = \"_5e5899d8398b5f7b\" )\n", "cursor = db.cursor()\n", "\n", "dict1 = {}\n", "\n", "sql = 'select account_name,account_number,parent_account from tabAccount where company= %s and parent_account is null'\n", "cursor.execute(sql, {'北京运动一百体育文化发展有限公司'})\n", "data = cursor.fetchall()\n", "for item in data:\n", " dict1.setdefault(item[1],{})\n", " dict1[item[1]]['name'] = item[0]\n", "# 获得科目分类\n", "#print(dict1)\n", "sql = 'select account_name,account_number,parent_account from tabAccount where company= %s and parent_account is not null and account_number is not null'\n", "cursor.execute(sql, {'北京运动一百体育文化发展有限公司'})\n", "data = cursor.fetchall()\n", "dict2 = {}\n", "for k, v in dict1.items():\n", " for item in data:\n", " parent = item[2].split(' - ')[0]\n", " if parent == k:\n", " dict2.setdefault(item[1],{})\n", " dict2[item[1]]['name'] = item[0]\n", "# 获得科目明细\n", "dict3 = {}\n", "\n", "for item in data:\n", " parent = item[2].split(' - ')[0]\n", " if len(item[1]) == 4 and parent in dict2.keys():\n", " dict3.setdefault(item[1],{})\n", " dict3[item[1]]['name'] = item[0]\n", " dict3[item[1]]['category'] = dict2[parent]['name']\n", " dict3[item[1]]['level'] = 1\n", " dict3[item[1]]['parent'] = ''\n", "for item in data:\n", " parent = item[2].split(' - ')[0]\n", " if len(item[1]) == 6 and parent in dict3.keys():\n", " dict3.setdefault(item[1],{})\n", " dict3[item[1]]['name'] = item[0]\n", " dict3[item[1]]['category'] = dict3[parent]['category']\n", " dict3[item[1]]['level'] = 2\n", " dict3[item[1]]['parent'] = parent \n", "for item in data:\n", " parent = item[2].split(' - ')[0]\n", " if len(item[1]) == 8 and parent in dict3.keys():\n", " dict3.setdefault(item[1],{})\n", " dict3[item[1]]['name'] = item[0]\n", " dict3[item[1]]['category'] = dict3[parent]['category']\n", " dict3[item[1]]['level'] = 3\n", " dict3[item[1]]['parent'] = parent \n", " \n", "for k, v in dict3.items():\n", " print(k,v['name'],v['category'],v['level'],v['parent'])" ] }, { "cell_type": "markdown", "id": "56e2b3b7-3c5d-4f42-afe3-bcf83b96ab2b", "metadata": {}, "source": [ "## 获取结账余额表" ] }, { "cell_type": "code", "execution_count": null, "id": "38c9169d-4dcb-44b1-b821-2b9a1fb55ec8", "metadata": {}, "outputs": [], "source": [ "import pymysql\n", "db = pymysql.connect(host = \"172.19.0.12\",user = \"songyi\",password = \"yylzs\",database = \"_5e5899d8398b5f7b\" )\n", "cursor = db.cursor()\n", "\n", "dict1 = {}\n", "\n", "sql = 'select account,debit,credit,period_closing_voucher from `tabAccount Closing Balance` where company= %s and closing_date = %s'\n", "cursor.execute(sql, ('北京运动一百体育文化发展有限公司','2023-03-31'))\n", "data = cursor.fetchall()\n", "for item in data:\n", " print(item)" ] }, { "cell_type": "markdown", "id": "b04ee7f2-bbdf-4749-9d38-a83eb479fa6b", "metadata": {}, "source": [ "## 获取关账凭证信息" ] }, { "cell_type": "code", "execution_count": null, "id": "bf427632-091e-4f9c-b711-c9a9a1930e54", "metadata": {}, "outputs": [], "source": [ "import pymysql\n", "from pymysql import converters, FIELD_TYPE\n", "\n", "conv = converters.conversions\n", "conv[FIELD_TYPE.NEWDECIMAL] = float\n", "conv[FIELD_TYPE.DATE] = str\n", "db = pymysql.connect(host = \"172.19.0.12\",user = \"songyi\",password = \"yylzs\",database = \"_5e5899d8398b5f7b\" )\n", "cursor = db.cursor()\n", "\n", "dict1 = {}\n", "\n", "sql = 'select name,transaction_date,remarks from `tabPeriod Closing Voucher` where company= %s and docstatus = %s'\n", "cursor.execute(sql, ('北京运动一百体育文化发展有限公司',1))\n", "data = cursor.fetchall()\n", "for item in data:\n", " print(item)" ] }, { "cell_type": "markdown", "id": "cfcca083-582e-430c-a2d8-bacffbe028de", "metadata": {}, "source": [ "## 获取凭证" ] }, { "cell_type": "code", "execution_count": null, "id": "8e62fbda-69b2-47f1-8bdd-e0a594b27d59", "metadata": {}, "outputs": [], "source": [ "import pymysql\n", "db = pymysql.connect(host = \"172.19.0.12\",user = \"songyi\",password = \"yylzs\",database = \"_5e5899d8398b5f7b\" )\n", "cursor = db.cursor()\n", "\n", "\n", "sql = 'select name,title,total_debit,total_credit from `tabJournal Entry` where company= %s and docstatus = %s'\n", "cursor.execute(sql, ('北京运动一百体育文化发展有限公司',1))\n", "data = cursor.fetchall()\n", "for item in data:\n", " print(item)\n" ] }, { "cell_type": "markdown", "id": "610ce516-e2f8-4955-9da0-323cbb0368cd", "metadata": {}, "source": [ "## 获取分录" ] }, { "cell_type": "code", "execution_count": null, "id": "489a83ab-4f5e-402a-abe2-4458acb414f4", "metadata": {}, "outputs": [], "source": [ "import pymysql\n", "from pymysql import converters, FIELD_TYPE\n", "\n", "conv = converters.conversions\n", "conv[FIELD_TYPE.NEWDECIMAL] = float\n", "conv[FIELD_TYPE.DATE] = str\n", "db = pymysql.connect(host = \"172.19.0.10\",user = \"songyi\",password = \"yylzs\",database = \"_5e5899d8398b5f7b\" )\n", "cursor = db.cursor()\n", "\n", "\n", "sql = 'select parent,account,debit,credit,against_account from `tabJournal Entry Account` where docstatus = %s and parent = %s'\n", "cursor.execute(sql, (1,'ACC-JV-2025-00099'))\n", "data = cursor.fetchall()\n", "for item in data:\n", " print(item)\n" ] }, { "cell_type": "markdown", "id": "5d6c1b37-d475-4762-b54d-2ad6e79c3945", "metadata": {}, "source": [ "## 获取总账" ] }, { "cell_type": "code", "execution_count": null, "id": "02129d62-ea3e-4492-89d0-bb023f746ff4", "metadata": {}, "outputs": [], "source": [ "import pymysql\n", "from pymysql import converters, FIELD_TYPE\n", "\n", "conv = converters.conversions\n", "conv[FIELD_TYPE.NEWDECIMAL] = float\n", "db = pymysql.connect(host = \"172.19.0.10\",user = \"songyi\",password = \"yylzs\",database = \"_5e5899d8398b5f7b\" )\n", "cursor = db.cursor()\n", "\n", "\n", "sql = 'select name,account,against,voucher_no,debit,credit from `tabGL Entry` where fiscal_year = %s'\n", "cursor.execute(sql, ('2025'))\n", "data = cursor.fetchall()\n", "for item in data:\n", " print(item)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "638cd779-7234-4445-b45e-cd6a2308f082", "metadata": {}, "outputs": [], "source": [ "import pymysql\n", "from pymysql import converters, FIELD_TYPE\n", "\n", "conv = converters.conversions\n", "conv[FIELD_TYPE.NEWDECIMAL] = float\n", "db = pymysql.connect(host = \"172.19.0.10\",user = \"songyi\",password = \"yylzs\",database = \"_5e5899d8398b5f7b\" )\n", "cursor = db.cursor()\n", "\n", "\n", "sql = 'select name,account,against,voucher_no,debit,credit from `tabGL Entry` where fiscal_year = %s and voucher_no = %s'\n", "cursor.execute(sql, ('2025','ACC-PCV-2025-00010'))\n", "data = cursor.fetchall()\n", "for item in data:\n", " print(item)\n" ] }, { "cell_type": "markdown", "id": "b349b31d-6dd3-489a-a3b0-c06f5100eb87", "metadata": {}, "source": [ "## 使用模板生成凭证" ] }, { "cell_type": "code", "execution_count": null, "id": "5a5828c7-9679-4694-9f4e-357ea71a5618", "metadata": {}, "outputs": [], "source": [ "import pymysql\n", "from pymysql import converters, FIELD_TYPE\n", "import openpyxl\n", "from docxtpl import DocxTemplate,InlineImage\n", "\n", "def num_to_rmb(num):\n", " # 定义中文数字和单位\n", " digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']\n", " units = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟']\n", " dec_units = ['角', '分']\n", " # 分离整数和小数部分\n", " integer_part = int(num)\n", " decimal_part = round((num - integer_part) * 100) # 保留两位小数\n", " # 处理整数部分\n", " if integer_part == 0:\n", " result = '零元'\n", " else:\n", " integer_str = str(integer_part)\n", " result = ''\n", " zero_flag = False # 标记是否需要添加零\n", " \n", " for i in range(len(integer_str)):\n", " digit = int(integer_str[i])\n", " pos = len(integer_str) - i - 1 # 当前位置的权值\n", " \n", " if digit == 0:\n", " zero_flag = True\n", " else:\n", " if zero_flag:\n", " result += '零'\n", " zero_flag = False\n", " result += digits[digit] + units[pos]\n", " \n", " result += '元' \n", " # 处理小数部分\n", " if decimal_part == 0:\n", " result += '整'\n", " else:\n", " jiao = decimal_part // 10\n", " fen = decimal_part % 10\n", " \n", " if jiao > 0:\n", " result += digits[jiao] + dec_units[0]\n", " elif integer_part > 0:\n", " result += '零'\n", " \n", " if fen > 0:\n", " result += digits[fen] + dec_units[1] \n", " # 特殊情况:\"壹拾\"开头的处理\n", " if result.startswith('壹拾'):\n", " result = result[1:] \n", " return '人民币' + result\n", "\n", "conv = converters.conversions\n", "conv[FIELD_TYPE.NEWDECIMAL] = float\n", "db = pymysql.connect(host = \"172.19.0.10\",user = \"songyi\",password = \"yylzs\",database = \"_5e5899d8398b5f7b\" )\n", "cursor = db.cursor()\n", "\n", "list1 = []\n", "tpl = DocxTemplate(\"data/tpl_pingzheng.docx\")\n", "sql = 'select parent,account,debit,credit,against_account from `tabJournal Entry Account` where docstatus = %s and parent = %s'\n", "cursor.execute(sql, (1,'ACC-JV-2025-00002-1'))\n", "data = cursor.fetchall()\n", "sum_debit = 0.00\n", "sum_credit = 0.00\n", "for item in data:\n", " dict1 = {}\n", " dict1['zhaiyao'] = item[0]\n", " dict1['kemu'] = item[1]\n", " if item[2] == 0.0:\n", " dict1['debit'] = '' \n", " else:\n", " dict1['debit'] = '{:.2f}'.format(item[2])\n", " sum_debit+= item[2]\n", " if item[3] == 0.0:\n", " dict1['credit'] = '' \n", " else:\n", " dict1['credit'] = '{:.2f}'.format(item[3])\n", " sum_credit+= item[3]\n", " list1.append(dict1) \n", "\n", "\n", "context = {}\n", "context['fenlu'] = list1\n", "context['sum_debit'] = '{:.2f}'.format(sum_debit)\n", "context['sum_credit'] = '{:.2f}'.format(sum_credit)\n", "tpl.render(context)\n", "tpl.save('data/test1.docx')" ] }, { "cell_type": "markdown", "id": "6f64acab-3580-4a4e-87c2-013df2c36d35", "metadata": {}, "source": [ "## 连接postgresql" ] }, { "cell_type": "code", "execution_count": 11, "id": "d2b44be1-f5a3-4a88-8a43-c99e2aea9924", "metadata": { "execution": { "iopub.execute_input": "2025-11-08T12:16:08.709098Z", "iopub.status.busy": "2025-11-08T12:16:08.708834Z", "iopub.status.idle": "2025-11-08T12:16:08.734946Z", "shell.execute_reply": "2025-11-08T12:16:08.734287Z", "shell.execute_reply.started": "2025-11-08T12:16:08.709077Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(None,)]\n" ] } ], "source": [ "import psycopg2\n", "\n", "# 建立数据库连接\n", "conn = psycopg2.connect(\n", "database=\"directus\",\n", "user=\"mydata\",\n", "password=\"songyi\",\n", "host=\"127.0.0.1\",\n", "port=\"5432\"\n", ")\n", "cur = conn.cursor()\n", "cur.execute(\"SELECT person ->'name' as name FROM tice_event\")\n", "rows = cur.fetchall()\n", "print(rows)\n", "conn.close()" ] }, { "cell_type": "code", "execution_count": null, "id": "a370a1aa-3f2c-48dc-b482-936bfd6b29ac", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }