From 34c948fb58b6dbf4f168f01318845a34dbe2ea3c Mon Sep 17 00:00:00 2001 From: "512song@sina.com" Date: Thu, 28 Jul 2022 08:57:03 +0800 Subject: [PATCH] 20220728 --- 文件操作.ipynb | 183 +++++++++++++++++++++++++++++------------ 文件管理1.ipynb | 213 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 336 insertions(+), 60 deletions(-) diff --git a/文件操作.ipynb b/文件操作.ipynb index f9caa91..622c1bb 100644 --- a/文件操作.ipynb +++ b/文件操作.ipynb @@ -282,6 +282,129 @@ "\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 邮件管理" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 使用网易邮箱群发邮件" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from email.mime.text import MIMEText\n", + "from email.mime.multipart import MIMEMultipart\n", + "from email.mime.application import MIMEApplication\n", + "from email.header import Header\n", + "import smtplib\n", + "import requests\n", + "import time\n", + "import re\n", + "import json\n", + "\n", + "\n", + " \n", + "\n", + "fl_name = 'data/低碳院报告.json'\n", + "\n", + "with open(fl_name,'r') as fl:\n", + " m_xx = json.load(fl)\n", + "for k, v in m_xx.items():\n", + " m_bh = str(k).rjust(5,\"0\")\n", + " fl_name = f'file/220724/new/{m_bh}-{v[0]}.pdf'\n", + " fl = f'{m_bh}-{v[0]}.pdf'\n", + " m_rec = v[2]+'@ceic.com'\n", + " \n", + " from_addr = 'kmingedu@163.com' #发件邮箱\n", + " password = 'GKWUZXMVYKSSUSSL' #邮箱密码\n", + " smtp_server = 'smtp.163.com' #SMTP服务器,以新浪为例\n", + " server = smtplib.SMTP(smtp_server,25) #第二个参数为默认端口为25,有些邮件有特殊端口\n", + " msg = MIMEMultipart()\n", + " msg['Subject'] = Header(\"低碳清洁能源研究院体质监测报告\",'utf-8')\n", + " msg['From'] = Header('北京坤铭体质监测评估中心')\n", + " msg['To'] = Header(m_rec)\n", + "\n", + " from_addr = 'kmingedu@163.com' #发件邮箱\n", + " password = 'GKWUZXMVYKSSUSSL' #邮箱密码\n", + " to_addr = m_rec #收件邮箱\n", + " att1 =MIMEApplication(open(fl_name, 'rb').read())\n", + " #att1[\"Content-Type\"] = 'application/octet-stream'\n", + " # 这里的filename可以任意写,写什么名字,邮件中显示什么名字\n", + " att1.add_header('Content-Disposition','attachment',filename=fl)\n", + " msg.attach(att1)\n", + " try:\n", + " server.login(from_addr,password) #登录邮箱\n", + " server.sendmail(from_addr,to_addr,msg.as_string()) #将msg转化成string发出\n", + " server.quit\n", + " time.sleep(5)\n", + " print(f'{fl}邮件发送成功!')\n", + " except smtplib.SMTPException:\n", + " print (\"Error: 无法发送邮件\")\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from email.mime.text import MIMEText\n", + "from email.mime.multipart import MIMEMultipart\n", + "from email.mime.application import MIMEApplication\n", + "from email.header import Header\n", + "import smtplib\n", + "import requests\n", + "import time\n", + "import re\n", + "import json\n", + "\n", + "\n", + "\n", + "from_addr = 'kmingedu@163.com' #发件邮箱\n", + "password = 'GKWUZXMVYKSSUSSL' #邮箱密码\n", + "smtp_server = 'smtp.163.com' #SMTP服务器,以新浪为例\n", + "server = smtplib.SMTP(smtp_server,25) #第二个参数为默认端口为25,有些邮件有特殊端口\n", + " \n", + "\n", + "fl_name = 'data/低碳院报告.json'\n", + "\n", + "with open(fl_name,'r') as fl:\n", + " m_xx = json.load(fl)\n", + "for k, v in m_xx.items():\n", + " m_bh = str(k).rjust(5,\"0\")\n", + " fl_name = f'file/220724/new/{m_bh}-{v[0]}.pdf'\n", + " fl = f'{m_bh}-{v[0]}.pdf'\n", + " m_rec = v[2]+'@ceic.com'\n", + " msg = MIMEMultipart()\n", + " msg['Subject'] = Header(\"低碳清洁能源研究院体质监测报告\",'utf-8')\n", + " msg['From'] = Header('北京坤铭体质监测评估中心')\n", + " msg['To'] = Header(m_rec)\n", + "\n", + " from_addr = 'kmingedu@163.com' #发件邮箱\n", + " password = 'GKWUZXMVYKSSUSSL' #邮箱密码\n", + " to_addr = v[2] #收件邮箱\n", + " att1 =MIMEApplication(open(fl_name, 'rb').read())\n", + " #att1[\"Content-Type\"] = 'application/octet-stream'\n", + " # 这里的filename可以任意写,写什么名字,邮件中显示什么名字\n", + " att1.add_header('Content-Disposition','attachment',filename=fl)\n", + " print(m_bh,v[0],m_rec,fl)" + ] + }, { "cell_type": "markdown", "metadata": { @@ -419,17 +542,9 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Topic List: ['arn:aws:sns:us-east-1:915521803346:MyTopic', 'arn:aws:sns:us-east-1:915521803346:dynamodb']\n" - ] - } - ], + "outputs": [], "source": [ "import boto3\n", "\n", @@ -521,17 +636,9 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'low': Decimal('1.29'), 'code': 'USDCAD', 'high': Decimal('1.32')}\n" - ] - } - ], + "outputs": [], "source": [ "import boto3\n", "# Get the service resource.\n", @@ -569,29 +676,9 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'ResponseMetadata': {'RequestId': 'LCHMG55BG6A89FF2KBSDM7JRHNVV4KQNSO5AEMVJF66Q9ASUAAJG',\n", - " 'HTTPStatusCode': 200,\n", - " 'HTTPHeaders': {'server': 'Server',\n", - " 'date': 'Thu, 30 Sep 2021 05:04:36 GMT',\n", - " 'content-type': 'application/x-amz-json-1.0',\n", - " 'content-length': '2',\n", - " 'connection': 'keep-alive',\n", - " 'x-amzn-requestid': 'LCHMG55BG6A89FF2KBSDM7JRHNVV4KQNSO5AEMVJF66Q9ASUAAJG',\n", - " 'x-amz-crc32': '2745614147'},\n", - " 'RetryAttempts': 0}}" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import boto3\n", "import decimal\n", @@ -612,17 +699,9 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Received and deleted message: {'MessageId': 'ecd656af-72e2-4bfc-9deb-3043dbe1bb80', 'ReceiptHandle': 'AQEBTx6u3PRfCgUy0SfLyOZcsD4fa5ge7AZvDTeQ/P4sOslg+sDMRYDZelJQOvxDK2XsLvy24DvD/n6GxBa2RwN55b/bnN7PxeYvv42d8GgkIy1dOPEBTytVKNIf+Z7t9+jMZMT2kq2ypXSa7jLFPFN00ArUkd0GI7nZ5SIS133s8exP46gRUGjgaS04CzAwPocTpWNt9GZpA114bvlicx2gNMYTIBsF65K2MzcyBoNVjLzmSJ1Pi0OCcQrAYaHvwzfnkriOBJLUX8BU4ksjWGJg0m8wh8hlPVMTLX+qxMneLzLawbf3cM/ricQR1XtooXrhsAuwAXA+VXaq5TzlpFJZaDZu5BvebF0Yj3rst7j54L/O3DwY0WfVmIrgv2yjOqL7', 'MD5OfBody': '9794e060a0eae4992dbf298549c42dc5', 'Body': 'https://sqs.us-east-1.amazonaws.com/915521803346/MySqs1', 'Attributes': {'SentTimestamp': '1632980620260'}}\n" - ] - } - ], + "outputs": [], "source": [ "import boto3\n", "\n", diff --git a/文件管理1.ipynb b/文件管理1.ipynb index fd3d8dc..7e0e53c 100755 --- a/文件管理1.ipynb +++ b/文件管理1.ipynb @@ -64,6 +64,210 @@ "#print(dict1)" ] }, + { + "cell_type": "markdown", + "id": "72866db8-89be-4498-b854-a3aefe4c897a", + "metadata": {}, + "source": [ + "## 低碳院人员信息转换" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "433c3f52-f4ae-4544-8d06-3636feae71ba", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import os,sys,shutil\n", + "import openpyxl\n", + "import math\n", + "\n", + "fi_xls = os.getcwd() + '/data/北京低碳监测.xlsx'\n", + "fi_path = os.getcwd() + '/file/220724'\n", + "old = []\n", + "dict1 = {}\n", + "\n", + "wb = openpyxl.load_workbook(fi_xls)\n", + "sheet = wb.active\n", + "depart = []\n", + "for n in range(2,sheet.max_row): \n", + " m_name = sheet.cell(n,6).value\n", + " m_depart = sheet.cell(n,3).value\n", + " if sheet.cell(n,4).value is not None:\n", + " dict1[int(sheet.cell(n,4).value)] = [m_name,m_depart]\n", + "\n", + "m_path = os.getcwd()+'/file/220724/new'\n", + "\n", + "fl=os.listdir(fi_path)\n", + "for fn in fl:\n", + " if os.path.isfile(fi_path + '/' + fn):\n", + " ofn = int(fn.split('.')[0])\n", + " old.append(ofn) \n", + "old.sort()\n", + "for n in old: \n", + " o_name = f'{fi_path}/{n}.pdf'\n", + " n_name = f'{fi_path}/new/{str(n).rjust(5,\"0\")}-{dict1[n][0]}.pdf'\n", + " if not os.path.exists(n_name):\n", + " shutil.copyfile(o_name,n_name)\n", + " print(n_name)\n", + "#print(dict1)" + ] + }, + { + "cell_type": "markdown", + "id": "cda8fbed-c0f2-4bfc-a23f-6b412bbcd0ba", + "metadata": {}, + "source": [ + "## 低碳院人员信息核验" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a34c9f6b-4ef3-4678-89bb-8b692e183968", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import os,sys,shutil\n", + "import openpyxl\n", + "import math\n", + "import json\n", + "\n", + "fi_xls = os.getcwd() + '/data/北京低碳监测.xlsx'\n", + "fi_path = os.getcwd() + '/file/220724'\n", + "old = []\n", + "dict1 = {}\n", + "dict2 = {}\n", + "dict3 = {}\n", + "wb = openpyxl.load_workbook(fi_xls)\n", + "sheet = wb.active\n", + "depart = []\n", + "for n in range(2,sheet.max_row+1): \n", + " m_name = sheet.cell(n,6).value\n", + " m_depart = sheet.cell(n,3).value\n", + " if sheet.cell(n,4).value is not None:\n", + " dict1[int(sheet.cell(n,4).value)] = [m_name,m_depart]\n", + "\n", + "fi_xls = os.getcwd() + '/data/1658883945637.xlsx'\n", + "wb = openpyxl.load_workbook(fi_xls)\n", + "sheet = wb.active\n", + "for n in range(2,sheet.max_row+1): \n", + " m_dm = sheet.cell(n,3).value\n", + " m_depart = sheet.cell(n,2).value\n", + " m_name = sheet.cell(n,4).value\n", + " if sheet.cell(n,3).value is not None:\n", + " dict2[sheet.cell(n,3).value] = [m_name,m_depart,m_dm]\n", + "#print(dict2)\n", + "fl=os.listdir(fi_path)\n", + "for fn in fl:\n", + " if os.path.isfile(fi_path + '/' + fn):\n", + " ofn = int(fn.split('.')[0])\n", + " old.append(ofn) \n", + "old.sort()\n", + "for n in old:\n", + " m_name = dict1[n][0]\n", + " m_depart = dict1[n][1]\n", + " for item in dict2.values():\n", + " if m_name in item and m_depart in item:\n", + " dict3[n] = [m_name,m_depart,item[2]]\n", + " break\n", + "for k in old:\n", + " if k not in dict3.keys():\n", + " print(k)\n", + "with open('data/低碳院报告.json','w') as fl2:\n", + " json.dump(dict3,fl2,ensure_ascii=False) " + ] + }, + { + "cell_type": "markdown", + "id": "3244663e-890c-48de-ae6a-1f666cb1bb76", + "metadata": {}, + "source": [ + "## 低碳院人员附件核验" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8e20ead8-754a-4535-95ea-b103264e3783", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import json\n", + "import os\n", + "\n", + "fl_name = 'data/低碳院报告.json'\n", + "\n", + "with open(fl_name,'r') as fl:\n", + " m_xx = json.load(fl)\n", + "for k, v in m_xx.items():\n", + " m_bh = str(k).rjust(5,\"0\")\n", + " fl_name = f'file/220724/new/{m_bh}-{v[0]}.pdf'\n", + " if os.path.exists(fl_name):\n", + " print(fl_name)\n", + " else:\n", + " print(f'{v[0]}不存在')" + ] + }, + { + "cell_type": "markdown", + "id": "a7976e2e-23fa-4464-89f5-0e96f721bc95", + "metadata": {}, + "source": [ + "## 低碳院人员导出" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "d77c3ff5-91fb-4449-80fb-57bc467c27bd", + "metadata": { + "execution": { + "iopub.execute_input": "2022-07-27T14:01:56.902136Z", + "iopub.status.busy": "2022-07-27T14:01:56.901602Z", + "iopub.status.idle": "2022-07-27T14:01:56.957533Z", + "shell.execute_reply": "2022-07-27T14:01:56.956472Z", + "shell.execute_reply.started": "2022-07-27T14:01:56.902088Z" + } + }, + "outputs": [], + "source": [ + "import json\n", + "import openpyxl\n", + " \n", + "\n", + "fl_name = 'data/低碳院报告.json'\n", + "\n", + "with open(fl_name,'r') as fl:\n", + " m_xx = json.load(fl)\n", + " \n", + "wb = openpyxl.Workbook()\n", + "sheet = wb.active\n", + "i = 0\n", + " \n", + "for k, v in m_xx.items():\n", + " m_bh = str(k).rjust(5,\"0\")\n", + " m_name = v[0]\n", + " fl = f'{m_bh}-{v[0]}.pdf'\n", + " m_rec = v[2]+'@ceic.com'\n", + " i += 1\n", + " sheet[f'A{i}'] = i\n", + " sheet[f'B{i}'] = m_bh\n", + " sheet[f'C{i}'] = m_name\n", + " sheet[f'D{i}'] = m_rec \n", + " sheet[f'E{i}'] = fl\n", + " \n", + "wb.save('data/低碳院邮件.xlsx') \n", + " " + ] + }, { "cell_type": "markdown", "id": "bbf0b425-c0ce-4608-ae52-85a78ddcd6c0", @@ -74,16 +278,9 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "2c2bd1d1-4d84-4639-ba20-c97c218ae275", "metadata": { - "execution": { - "iopub.execute_input": "2022-05-06T14:01:25.901198Z", - "iopub.status.busy": "2022-05-06T14:01:25.900687Z", - "iopub.status.idle": "2022-05-06T14:01:25.911729Z", - "shell.execute_reply": "2022-05-06T14:01:25.910606Z", - "shell.execute_reply.started": "2022-05-06T14:01:25.901153Z" - }, "tags": [] }, "outputs": [],