{ "cells": [ { "cell_type": "markdown", "id": "0f939dd1-de12-4b54-bd08-16ebbbfdbfdd", "metadata": {}, "source": [ "## 选择白子黑子图片" ] }, { "cell_type": "code", "execution_count": null, "id": "0569e182-b008-4173-ab0d-7f4a2904f72e", "metadata": { "tags": [] }, "outputs": [], "source": [ "import glob\n", "import os,shutil\n", "from pathlib import Path\n", "\n", "filepath = 'img/白/'\n", "\n", "files = glob.glob(f'{filepath}*.jpg')\n", "for fn in files:\n", " fi_name =Path(fn).stem\n", " if int(fi_name) % 2 !=0:\n", " shutil.copy(fn, './img')" ] }, { "cell_type": "code", "execution_count": null, "id": "44683ca5-166c-4d3a-9447-6fe46ab7c12d", "metadata": { "tags": [] }, "outputs": [], "source": [ "import glob\n", "import os,shutil\n", "from pathlib import Path\n", "\n", "filepath = 'img/黑/'\n", "\n", "files = glob.glob(f'{filepath}*.jpg')\n", "for fn in files:\n", " fi_name =Path(fn).stem\n", " if int(fi_name) % 2 ==0:\n", " shutil.copy(fn, './img')" ] }, { "cell_type": "markdown", "id": "230c5cb3-ee5b-4490-b0a2-6eb62461204b", "metadata": {}, "source": [ "## 棋子替代" ] }, { "cell_type": "code", "execution_count": null, "id": "3ecfe0a8-3af1-4f48-a2b0-6c3eb25df07d", "metadata": { "tags": [] }, "outputs": [], "source": [ "import glob\n", "import os,shutil\n", "from pathlib import Path\n", "import re\n", "\n", "def find_all_numbers(text):\n", " return re.findall(r'\\d+', text)\n", "\n", "def replace_numbers(text):\n", " pattern = r'\\d+'\n", " replacement = lambda x:'{{ '+ f'img_{x.group()}'+' }}'\n", " modified_text = re.sub(pattern, replacement, text)\n", " return modified_text\n", "\n", "def replace_numbers_with_braces(text):\n", " # 使用正则表达式查找连续的数字\n", " pattern = re.compile(r'\\d+')\n", " \n", " # 使用re.sub()进行替换\n", " result = pattern.sub(lambda x: f\"{{{{ img_{x.group(0)} }}}}\", text)\n", " \n", " return result\n", "\n", "list1 = []\n", "filepath = '文本文件/'\n", "files = glob.glob(f'{filepath}*.md')\n", "for fn in files:\n", " with open(fn, \"r\") as f:\n", " data = f.readlines()\n", " for s in data:\n", " ss = replace_numbers_with_braces(s)\n", " list1 = find_all_numbers(ss)\n", " list1 = list(set(list1))\n", " list1.sort()\n", " print(ss,list1)" ] }, { "cell_type": "code", "execution_count": null, "id": "14696c7a-d44e-4c99-9d28-a810d991ed20", "metadata": {}, "outputs": [], "source": [ "import glob\n", "import os,shutil\n", "from pathlib import Path\n", "import re\n", "from docx import Document\n", "import openpyxl\n", "from docxtpl import DocxTemplate,InlineImage\n", "from docx.shared import Mm\n", "\n", "def find_all_numbers(text):\n", " return re.findall(r'\\d+', text)\n", "\n", "def replace_numbers(text):\n", " pattern = r'\\d+'\n", " replacement = lambda x:'{{ '+ f'img_{x.group()}'+' }}'\n", " modified_text = re.sub(pattern, replacement, text)\n", " return modified_text\n", "\n", "def replace_numbers_with_braces(text):\n", " # 使用正则表达式查找连续的数字\n", " pattern = re.compile(r'\\d+')\n", " \n", " # 使用re.sub()进行替换\n", " result = pattern.sub(lambda x: f\"{{{{ img_{x.group(0)} }}}}\", text)\n", " \n", " return result\n", "#doc = Document()\n", "\n", "\n", "filepath = '文本文件/'\n", "files = glob.glob(f'{filepath}*.txt')\n", "for fn in files:\n", " p = Path(fn)\n", " with open(fn, \"r\") as f:\n", " list1 = []\n", " doc = Document()\n", " paragraph3 = doc.add_paragraph()\n", " data = f.readlines()\n", " for s in data:\n", " list2 = []\n", " ss = replace_numbers_with_braces(s)\n", " list2 = find_all_numbers(s)\n", " if len(list2)>0:\n", " for item in list2:\n", " list1.append(item)\n", " \n", " list1 = list(set(list1))\n", " list1.sort()\n", " #paragraph3 = doc.add_paragraph()\n", " paragraph3.add_run(ss.replace('\\r', ''))\n", " print(p.stem)\n", " print(list1)\n", " if len(list1) >0:\n", " #paragraph3 = doc.add_paragraph()\n", " #paragraph3.add_run(ss.replace('\\r', ''))\n", " doc.save(f'./templete/{p.stem}.docx')\n", " dict1 = {}\n", " tpl = DocxTemplate(f'./templete/{p.stem}.docx')\n", " for item in list1:\n", " dict1['img_'+str(item)] = InlineImage(tpl, image_descriptor=f'./img/{str(item)}.jpg',width=Mm(4))\n", " tpl.render(dict1)\n", " tpl.save(f'./docx/{p.stem}.docx')\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "ad5af12a-a322-4963-afc7-e3fdfbd9a4ab", "metadata": { "execution": { "iopub.execute_input": "2024-08-29T10:02:06.727393Z", "iopub.status.busy": "2024-08-29T10:02:06.726626Z", "iopub.status.idle": "2024-08-29T10:02:06.755461Z", "shell.execute_reply": "2024-08-29T10:02:06.754846Z", "shell.execute_reply.started": "2024-08-29T10:02:06.727321Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['10', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '11', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '12', '120', '121', '122', '123', '124', '125', '126', '127', '129', '13', '130', '131', '132', '133', '134', '135', '136', '137', '139', '14', '140', '141', '142', '143', '144', '145', '148', '149', '15', '150', '151', '152', '153', '154', '155', '156', '157', '159', '16', '160', '162', '163', '164', '165', '166', '169', '17', '170', '171', '172', '173', '174', '175', '176', '177', '178', '18', '180', '181', '182', '183', '184', '185', '186', '189', '19', '190', '191', '192', '194', '195', '196', '197', '198', '199', '2', '20', '200', '201', '202', '203', '204', '206', '208', '209', '21', '210', '211', '212', '213', '218', '219', '22', '220', '221', '23', '233', '237', '24', '240', '243', '25', '250', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '4', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '5', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '8', '80', '81', '82', '83', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99']\n" ] } ], "source": [ "import glob\n", "import os,shutil\n", "from pathlib import Path\n", "import re\n", "from docx import Document\n", "import openpyxl\n", "from docxtpl import DocxTemplate,InlineImage\n", "from docx.shared import Mm\n", "\n", "def find_all_numbers(text):\n", " return re.findall(r'\\d+', text)\n", "\n", "def replace_numbers(text):\n", " pattern = r'\\d+'\n", " replacement = lambda x:'{{ '+ f'img_{x.group()}'+' }}'\n", " modified_text = re.sub(pattern, replacement, text)\n", " return modified_text\n", "\n", "def replace_numbers_with_braces(text):\n", " # 使用正则表达式查找连续的数字\n", " pattern = re.compile(r'\\d+')\n", " \n", " # 使用re.sub()进行替换\n", " result = pattern.sub(lambda x: f\"{{{{ img_{x.group(0)} }}}}\", text)\n", " \n", " return result\n", "fn = '文本文件/槐荫堂7.txt'\n", "with open(fn, \"r\") as f:\n", " list1 = []\n", " \n", " data = f.readlines()\n", " for s in data:\n", " list2 = []\n", " ss = replace_numbers_with_braces(s)\n", " list2 = find_all_numbers(s)\n", " if len(list2)>0:\n", " for item in list2:\n", " list1.append(item)\n", " \n", " list1 = list(set(list1))\n", " list1.sort()\n", " \n", " print(list1)" ] }, { "cell_type": "code", "execution_count": 3, "id": "8d3f1a7a-1322-48b5-9b14-c7ba8f38bacb", "metadata": { "execution": { "iopub.execute_input": "2024-08-29T10:02:13.702443Z", "iopub.status.busy": "2024-08-29T10:02:13.701668Z", "iopub.status.idle": "2024-08-29T10:02:15.040667Z", "shell.execute_reply": "2024-08-29T10:02:15.040126Z", "shell.execute_reply.started": "2024-08-29T10:02:13.702372Z" }, "tags": [] }, "outputs": [], "source": [ "import openpyxl\n", "from docxtpl import DocxTemplate,InlineImage\n", "from docx.shared import Mm\n", "\n", "dict1 = {}\n", "\n", "tpl = DocxTemplate(\"templete/槐荫堂7.docx\")\n", "for item in list1: \n", " dict1['img_'+str(item)] = InlineImage(tpl, image_descriptor=f'./img/黑先/{str(item)}.jpg',width=Mm(4))\n", "#dict1['img_radar'] = InlineImage(tpl, image_descriptor=dict1['radar'])\n", "\n", "tpl.render(dict1)\n", "tpl.save('./docx/槐荫堂7(黑先).docx')" ] }, { "cell_type": "code", "execution_count": null, "id": "efc716f8-0ec7-4e0d-9960-676764965be4", "metadata": {}, "outputs": [], "source": [ "import openpyxl\n", "from docxtpl import DocxTemplate,InlineImage\n", "from docx.shared import Mm\n", "\n", "dict1 = {}\n", "\n", "tpl = DocxTemplate(\"离垢居谈棋.docx\")\n", "for item in list1: \n", " dict1['img_'+str(item)] = InlineImage(tpl, image_descriptor=f'./img/白先/{str(item)}.jpg',width=Mm(4))\n", "#dict1['img_radar'] = InlineImage(tpl, image_descriptor=dict1['radar'])\n", "\n", "tpl.render(dict1)\n", "tpl.save('离垢居谈棋(白先).docx')" ] }, { "cell_type": "markdown", "id": "bd66efe6-1949-415c-aef3-7f953e1cf79f", "metadata": {}, "source": [ "## Excel文件内容生成markdown格式文本" ] }, { "cell_type": "code", "execution_count": null, "id": "117302bc-3b7f-4456-9770-d8da80594c4e", "metadata": {}, "outputs": [], "source": [ "import openpyxl\n", "\n", "fi_xls = 'data/对局目录.xlsx'\n", "\n", "wb = openpyxl.load_workbook(fi_xls)\n", "sheet = wb['卷18'] \n", "\n", "\n", "for n in range(sheet.max_row - 1,sheet.max_row+1): \n", " print('## 第'+sheet.cell(n,1).value+'局 ')\n", " print(sheet.cell(n,2).value+' ')\n", " print(sheet.cell(n,3).value+' ')\n", " print('共'+sheet.cell(n,4).value+'着 ')\n", " if sheet.cell(n,5).value is not None:\n", " print(sheet.cell(n,5).value+' ')\n", " print(sheet.cell(n,6).value+' ')\n", " print('\\n')" ] }, { "cell_type": "code", "execution_count": 4, "id": "b3adea5a-d9da-45f8-be10-b0973acc7426", "metadata": { "execution": { "iopub.execute_input": "2024-09-01T03:00:15.506058Z", "iopub.status.busy": "2024-09-01T03:00:15.505309Z", "iopub.status.idle": "2024-09-01T03:00:15.633772Z", "shell.execute_reply": "2024-09-01T03:00:15.632885Z", "shell.execute_reply.started": "2024-09-01T03:00:15.505989Z" } }, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'data/对局目录.xlsx'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[4], line 5\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mopenpyxl\u001b[39;00m\n\u001b[1;32m 3\u001b[0m fi_xls \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdata/对局目录.xlsx\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m----> 5\u001b[0m wb \u001b[38;5;241m=\u001b[39m \u001b[43mopenpyxl\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mload_workbook\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfi_xls\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 6\u001b[0m sheet \u001b[38;5;241m=\u001b[39m wb[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m卷24\u001b[39m\u001b[38;5;124m'\u001b[39m] \n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m n \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(sheet\u001b[38;5;241m.\u001b[39mmax_row \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m50\u001b[39m): \n", "File \u001b[0;32m~/data/lib/python3.12/site-packages/openpyxl/reader/excel.py:344\u001b[0m, in \u001b[0;36mload_workbook\u001b[0;34m(filename, read_only, keep_vba, data_only, keep_links, rich_text)\u001b[0m\n\u001b[1;32m 314\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mload_workbook\u001b[39m(filename, read_only\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, keep_vba\u001b[38;5;241m=\u001b[39mKEEP_VBA,\n\u001b[1;32m 315\u001b[0m data_only\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, keep_links\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, rich_text\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m):\n\u001b[1;32m 316\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Open the given filename and return the workbook\u001b[39;00m\n\u001b[1;32m 317\u001b[0m \n\u001b[1;32m 318\u001b[0m \u001b[38;5;124;03m :param filename: the path to open or a file-like object\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 342\u001b[0m \n\u001b[1;32m 343\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 344\u001b[0m reader \u001b[38;5;241m=\u001b[39m \u001b[43mExcelReader\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfilename\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mread_only\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeep_vba\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 345\u001b[0m \u001b[43m \u001b[49m\u001b[43mdata_only\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeep_links\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrich_text\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 346\u001b[0m reader\u001b[38;5;241m.\u001b[39mread()\n\u001b[1;32m 347\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m reader\u001b[38;5;241m.\u001b[39mwb\n", "File \u001b[0;32m~/data/lib/python3.12/site-packages/openpyxl/reader/excel.py:123\u001b[0m, in \u001b[0;36mExcelReader.__init__\u001b[0;34m(self, fn, read_only, keep_vba, data_only, keep_links, rich_text)\u001b[0m\n\u001b[1;32m 121\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\u001b[38;5;28mself\u001b[39m, fn, read_only\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, keep_vba\u001b[38;5;241m=\u001b[39mKEEP_VBA,\n\u001b[1;32m 122\u001b[0m data_only\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, keep_links\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, rich_text\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m):\n\u001b[0;32m--> 123\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39marchive \u001b[38;5;241m=\u001b[39m \u001b[43m_validate_archive\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 124\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvalid_files \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39marchive\u001b[38;5;241m.\u001b[39mnamelist()\n\u001b[1;32m 125\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mread_only \u001b[38;5;241m=\u001b[39m read_only\n", "File \u001b[0;32m~/data/lib/python3.12/site-packages/openpyxl/reader/excel.py:95\u001b[0m, in \u001b[0;36m_validate_archive\u001b[0;34m(filename)\u001b[0m\n\u001b[1;32m 88\u001b[0m msg \u001b[38;5;241m=\u001b[39m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mopenpyxl does not support \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m file format, \u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 89\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mplease check you can open \u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 90\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mit with Excel first. \u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[1;32m 91\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mSupported formats are: \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m%\u001b[39m (file_format,\n\u001b[1;32m 92\u001b[0m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m,\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(SUPPORTED_FORMATS))\n\u001b[1;32m 93\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m InvalidFileException(msg)\n\u001b[0;32m---> 95\u001b[0m archive \u001b[38;5;241m=\u001b[39m \u001b[43mZipFile\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfilename\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 96\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m archive\n", "File \u001b[0;32m/usr/lib/python3.12/zipfile/__init__.py:1331\u001b[0m, in \u001b[0;36mZipFile.__init__\u001b[0;34m(self, file, mode, compression, allowZip64, compresslevel, strict_timestamps, metadata_encoding)\u001b[0m\n\u001b[1;32m 1329\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[1;32m 1330\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 1331\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mfp \u001b[38;5;241m=\u001b[39m \u001b[43mio\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfilemode\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1332\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mOSError\u001b[39;00m:\n\u001b[1;32m 1333\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m filemode \u001b[38;5;129;01min\u001b[39;00m modeDict:\n", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'data/对局目录.xlsx'" ] } ], "source": [ "import openpyxl\n", "\n", "fi_xls = 'data/对局目录.xlsx'\n", "\n", "wb = openpyxl.load_workbook(fi_xls)\n", "sheet = wb['卷24'] \n", "\n", "\n", "for n in range(sheet.max_row - 1,50): \n", " print(sheet.cell(n,1).value+' ')\n", " print(sheet.cell(n,2).value+' ')\n", " print(sheet.cell(n,3).value+' ')\n", " print(sheet.cell(n,4).value+' ')\n", " \n", " print('\\n')" ] }, { "cell_type": "code", "execution_count": null, "id": "ce35a953-51bc-450b-8af6-9f68edc2f9b1", "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 }