{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fd67cc1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\joao.herculano\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\\openpyxl\\worksheet\\_reader.py:329: UserWarning: Unknown extension is not supported and will be removed\n",
      "  warn(msg)\n",
      "c:\\Users\\joao.herculano\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\\openpyxl\\worksheet\\_reader.py:329: UserWarning: Unknown extension is not supported and will be removed\n",
      "  warn(msg)\n",
      "c:\\Users\\joao.herculano\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\\openpyxl\\worksheet\\_reader.py:329: UserWarning: Unknown extension is not supported and will be removed\n",
      "  warn(msg)\n",
      "c:\\Users\\joao.herculano\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\site-packages\\openpyxl\\worksheet\\_reader.py:329: UserWarning: Unknown extension is not supported and will be removed\n",
      "  warn(msg)\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import glob\n",
    "import pandas as pd\n",
    "\n",
    "# Directory path\n",
    "folder_path = r\"C:\\Users\\joao.herculano\\GRUPO GINSENG\\Assistência Suprimentos - 2025\\SUPRIMENTOS\\BD_LANÇAMENTOS\\BOT\\Bot - C13\\LANÇAMENTOS\\devolutiva comercial\"\n",
    "# Collect files\n",
    "file_paths = glob.glob(os.path.join(folder_path, \"*.xls\")) + glob.glob(os.path.join(folder_path, \"*.xlsx\"))\n",
    "\n",
    "# Required columns\n",
    "required_columns = [\"PDV\", \"PRODUTO LANÇAMENTO\", \"SUGESTÃO COMERCIAL\"]\n",
    "\n",
    "# Load files\n",
    "dfs = []\n",
    "for file_path in file_paths:\n",
    "    try:\n",
    "        df = pd.read_excel(file_path,sheet_name='Sheet1')\n",
    "        \n",
    "        missing = [col for col in required_columns if col not in df.columns]\n",
    "        \n",
    "        if missing:\n",
    "            print(f\"Missing columns {missing} ins file: {file_path}\")\n",
    "            continue  # Skip file with missing columns\n",
    "\n",
    "        df[\"source_file\"] = os.path.basename(file_path)\n",
    "        dfs.append(df)\n",
    "\n",
    "    except Exception as e:\n",
    "        print(f\"Failed to read {file_path}: {e}\")\n",
    "\n",
    "# Combine\n",
    "if dfs:\n",
    "    combined_df = pd.concat(dfs, ignore_index=True)\n",
    "else:\n",
    "    print(\"No valid files loaded.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c4c50096",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(82418)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "\n",
    "combined_df['SUGESTÃO COMERCIAL'].isna().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3e1ada65",
   "metadata": {},
   "outputs": [],
   "source": [
    "combined_df = combined_df.drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f5968d9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "combined_df['chavers'] = combined_df['PDV'].astype('str') + combined_df['PRODUTO LANÇAMENTO'].astype('str')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fd6a08e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "combined_df['chavers'] = combined_df['chavers'].str.replace('.','')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b6eee60e",
   "metadata": {},
   "outputs": [],
   "source": [
    "combined_df.to_excel(r\"C:\\Users\\joao.herculano\\GRUPO GINSENG\\Assistência Suprimentos - 2025\\SUPRIMENTOS\\BD_LANÇAMENTOS\\BOT\\Bot - C13\\LANÇAMENTOS\\compilado_comercial_BOTI!!!.xlsx\",index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84f989f7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
