{ "cells": [ { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['工作表1', 'Sheet1']" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "res = list()\n", "\n", "plain = pd.ExcelFile('TestA.xlsx')\n", "for sheet_name in plain.sheet_names:\n", " df1 = pd.read_excel('TestA.xlsx', sheet_name=sheet_name)\n", " df2 = pd.read_excel('TestB.xlsx', sheet_name=sheet_name)\n", "\n", " comparison_values = df1.values == df2.values\n", " comparison_values\n", "\n", " rows,cols=np.where(comparison_values==False)\n", "\n", " for item in zip(rows,cols):\n", " df1.iloc[item[0], item[1]] = '{} --> {}'.format(df1.iloc[item[0], item[1]],df2.iloc[item[0], item[1]])\n", " \n", " res.append(df1)\n", " # df1.to_excel('new.xlsx', sheet_name=sheet_name)\n", " \n", "\n", "with pd.ExcelWriter('new.xlsx') as writer:\n", " for index in range(len(res)):\n", " res[index].to_excel(writer, sheet_name=plain.sheet_names[index])\n", "\n", "pd.ExcelFile('new.xlsx').sheet_names" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.0 64-bit", "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.9.0" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "fdf377d643bc1cb065454f0ad2ceac75d834452ecf289e7ba92c6b3f59a7cee1" } } }, "nbformat": 4, "nbformat_minor": 2 }