{ "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}*.txt')\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": 36, "id": "14696c7a-d44e-4c99-9d28-a810d991ed20", "metadata": { "execution": { "iopub.execute_input": "2024-07-29T09:38:17.617646Z", "iopub.status.busy": "2024-07-29T09:38:17.616877Z", "iopub.status.idle": "2024-07-29T09:38:17.699602Z", "shell.execute_reply": "2024-07-29T09:38:17.699000Z", "shell.execute_reply.started": "2024-07-29T09:38:17.617576Z" } }, "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', '128', '129', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '14', '140', '141', '142', '143', '144', '145', '146', '147', '148', '149', '15', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '16', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '17', '170', '171', '173', '174', '175', '176', '177', '178', '179', '18', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189', '19', '190', '191', '192', '193', '195', '198', '2', '20', '200', '201', '203', '204', '209', '21', '212', '213', '214', '22', '221', '222', '227', '228', '23', '230', '232', '24', '242', '243', '245', '246', '248', '249', '25', '251', '254', '255', '26', '27', '276', '28', '29', '290', '293', '3', '30', '304', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '6', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '7', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '8', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '9', '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", "\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", "list1 = []\n", "filepath = '文本文件/'\n", "files = glob.glob(f'{filepath}*.txt')\n", "for fn in files:\n", " with open(fn, \"r\") as f:\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", " \n", "print(list1)\n", "doc.save('槐荫堂9.docx')" ] }, { "cell_type": "code", "execution_count": 37, "id": "8d3f1a7a-1322-48b5-9b14-c7ba8f38bacb", "metadata": { "execution": { "iopub.execute_input": "2024-07-29T09:38:34.133722Z", "iopub.status.busy": "2024-07-29T09:38:34.132997Z", "iopub.status.idle": "2024-07-29T09:38:36.600350Z", "shell.execute_reply": "2024-07-29T09:38:36.599803Z", "shell.execute_reply.started": "2024-07-29T09:38:34.133659Z" }, "tags": [] }, "outputs": [], "source": [ "import openpyxl\n", "from docxtpl import DocxTemplate,InlineImage\n", "from docx.shared import Mm\n", "\n", "dict1 = {}\n", "\n", "tpl = DocxTemplate(\"槐荫堂9.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": "code", "execution_count": null, "id": "efc716f8-0ec7-4e0d-9960-676764965be4", "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 }