Python自动化办公模板源码,在Excel中按条件筛选数据并存入新的表

{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "老板想要看去年每月领料数量大于1000的数据。手动筛选并复制粘贴出来,需要重复操作12次,实在太麻烦了,还是让Python来做吧。磨刀不误砍柴工,先整理一下思路:\n",
    "1. 读取原表,将数量大于1000的数据所对应的行整行提取(如同在excel表中按数字筛选大于1000的)\n",
    "2. 将提取的数据写入新的Excel表\n",
    "![](images\\problem.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#1.获取满足条件的数据\n",
    "from openpyxl import load_workbook\n",
    "wb = load_workbook(\"每月物料表.xlsx\")\n",
    "data = {} #储存所有工作表中满足条件的数据,以工作表名称为键\n",
    "sheet_names = wb.sheetnames\n",
    "for sheet_name in sheet_names:\n",
    "    ws = wb[sheet_name]\n",
    "    qty_list = []\n",
    "    #获取G列的数据,并用enumrate给其对应的元素编号\n",
    "    for row in range(2,ws.max_row+1):\n",
    "        qty = ws['G'+str(row)].value\n",
    "        qty_list.append(qty)\n",
    "\n",
    "    qty_idx = list(enumerate(qty_list)) #用于编号\n",
    "    \n",
    "    #判断数据是否大于1000,然后返回大于1000的数据所对应的行数\n",
    "    row_idx = [] #用于储存数量大于1000所对应的的行号\n",
    "    for i in range(len(qty_idx)):\n",
    "        if qty_idx[i][1] > 1000:\n",
    "            row_idx.append(qty_idx[i][0]+2)\n",
    "\n",
    "    #获取满足条件的数据        \n",
    "    data_morethan1K = []\n",
    "    for i in row_idx:\n",
    "        data_morethan1K.append(ws['A'+str(i)+\":\"+'I'+str(i)])\n",
    "        \n",
    "    data[sheet_name]=data_morethan1K "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以上,我们把满足条件的12个月的数据提取并存入字典`data`,其键为对应的月份,比如“1月”,值就是满足条件的各行的数据。我们把“每月物料表”的G列对应的数据提取,存入列表`qty_list`,其中前10个数据是如下这样的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[8, 72, 72, 8, 16, 93, 56, 63, 80, 30]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qty_list[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后需要使用`enumerate`函数给这个列表的数据加上索引,以便在跟1000比大小的时候定位满足条件的那些数据的对应在Excel表中的行数。加上索引之后的列表是如下这样的,索引从0开始累加。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(0, 8),\n",
       " (1, 72),\n",
       " (2, 72),\n",
       " (3, 8),\n",
       " (4, 16),\n",
       " (5, 93),\n",
       " (6, 56),\n",
       " (7, 63),\n",
       " (8, 80),\n",
       " (9, 30)]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qty_idx[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后,再新建一个列表`row_idx`,用于储存“领料数量”大于1000的数据所对应的行号。此处用到`if`语句进行判断,只将“领料数量”大于1000的数据所对应的行号加上2存入列表。为什么要加2,是因为`range`函数是从0开始取的,然后工作表首行是字段名,第二行开始才是数据。如下结果显示了满足条件的数据对应的行数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[317, 550, 551, 556, 557]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row_idx[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " 然后新建列表`data_morethan1K`用于存储以上行号对应的整行数据。比如`ws['A1:I1']`就指第一行从A列到I列的所有单元格数据。最后将数据存入`data`字典中。数据结构如下所示。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((,\n",
       "  ,\n",
       "  ,\n",
       "  ,\n",
       "  ,\n",
       "  ,\n",
       "  ,\n",
       "  ,\n",
       "  ),)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_morethan1K[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[((,\n",
       "   ,\n",
       "    
 

你可能感兴趣的:(windows,linux,服务器)