243 lines
8.3 KiB
Plaintext
243 lines
8.3 KiB
Plaintext
{
|
|
"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": 10,
|
|
"id": "14696c7a-d44e-4c99-9d28-a810d991ed20",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-07-20T06:04:52.673744Z",
|
|
"iopub.status.busy": "2024-07-20T06:04:52.672966Z",
|
|
"iopub.status.idle": "2024-07-20T06:04:52.992448Z",
|
|
"shell.execute_reply": "2024-07-20T06:04:52.991874Z",
|
|
"shell.execute_reply.started": "2024-07-20T06:04:52.673671Z"
|
|
}
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"['1', '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', '13', '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', '172', '173', '174', '175', '176', '177', '178', '179', '18', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189', '19', '190', '191', '192', '193', '194', '195', '196', '197', '198', '199', '2', '20', '200', '201', '202', '203', '204', '205', '206', '207', '208', '209', '21', '210', '211', '212', '213', '214', '215', '216', '217', '218', '219', '22', '220', '221', '222', '223', '224', '225', '226', '227', '228', '229', '23', '230', '231', '233', '234', '235', '236', '237', '238', '239', '24', '240', '242', '243', '244', '245', '246', '247', '248', '249', '25', '251', '252', '253', '254', '255', '256', '257', '258', '26', '260', '261', '262', '265', '266', '267', '268', '269', '27', '270', '272', '273', '274', '275', '276', '278', '28', '284', '29', '298', '3', '30', '31', '310', '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', '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('槐荫堂2.docx')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"id": "8d3f1a7a-1322-48b5-9b14-c7ba8f38bacb",
|
|
"metadata": {
|
|
"execution": {
|
|
"iopub.execute_input": "2024-07-20T06:06:38.179808Z",
|
|
"iopub.status.busy": "2024-07-20T06:06:38.179038Z",
|
|
"iopub.status.idle": "2024-07-20T06:06:57.642154Z",
|
|
"shell.execute_reply": "2024-07-20T06:06:57.641700Z",
|
|
"shell.execute_reply.started": "2024-07-20T06:06:38.179737Z"
|
|
},
|
|
"tags": []
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import openpyxl\n",
|
|
"from docxtpl import DocxTemplate,InlineImage\n",
|
|
"from docx.shared import Mm\n",
|
|
"\n",
|
|
"dict1 = {}\n",
|
|
"\n",
|
|
"tpl = DocxTemplate(\"槐荫堂2.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
|
|
}
|