Files
2024-09-18 10:19:45 +08:00

20 KiB

选择白子黑子图片

In [ ]:
import glob
import os,shutil
from pathlib import Path

filepath = 'img/白/'

files = glob.glob(f'{filepath}*.jpg')
for fn in files:
    fi_name =Path(fn).stem
    if int(fi_name) % 2 !=0:
        shutil.copy(fn, './img')
In [ ]:
import glob
import os,shutil
from pathlib import Path

filepath = 'img/黑/'

files = glob.glob(f'{filepath}*.jpg')
for fn in files:
    fi_name =Path(fn).stem
    if int(fi_name) % 2 ==0:
        shutil.copy(fn, './img')

棋子替代

In [ ]:
import glob
import os,shutil
from pathlib import Path
import re

def find_all_numbers(text):
    return re.findall(r'\d+', text)

def replace_numbers(text):
    pattern = r'\d+'
    replacement = lambda x:'{{ '+ f'img_{x.group()}'+' }}'
    modified_text = re.sub(pattern, replacement, text)
    return modified_text

def replace_numbers_with_braces(text):
    # 使用正则表达式查找连续的数字
    pattern = re.compile(r'\d+')
    
    # 使用re.sub()进行替换
    result = pattern.sub(lambda x: f"{{{{ img_{x.group(0)} }}}}", text)
    
    return result

list1 = []
filepath = '文本文件/'
files = glob.glob(f'{filepath}*.md')
for fn in files:
    with open(fn, "r") as f:
        data = f.readlines()
        for s in data:
            ss = replace_numbers_with_braces(s)
            list1 = find_all_numbers(ss)
            list1 = list(set(list1))
            list1.sort()
            print(ss,list1)
In [ ]:
import glob
import os,shutil
from pathlib import Path
import re
from docx import Document
import openpyxl
from docxtpl import DocxTemplate,InlineImage
from docx.shared import Mm

def find_all_numbers(text):
    return re.findall(r'\d+', text)

def replace_numbers(text):
    pattern = r'\d+'
    replacement = lambda x:'{{ '+ f'img_{x.group()}'+' }}'
    modified_text = re.sub(pattern, replacement, text)
    return modified_text

def replace_numbers_with_braces(text):
    # 使用正则表达式查找连续的数字
    pattern = re.compile(r'\d+')
    
    # 使用re.sub()进行替换
    result = pattern.sub(lambda x: f"{{{{ img_{x.group(0)} }}}}", text)
    
    return result
#doc = Document()


filepath = '文本文件/'
files = glob.glob(f'{filepath}*.txt')
for fn in files:
    p = Path(fn)
    with open(fn, "r") as f:
        list1 = []
        doc = Document()
        paragraph3 = doc.add_paragraph()
        data = f.readlines()
        for s in data:
            list2 = []
            ss = replace_numbers_with_braces(s)
            list2 = find_all_numbers(s)
            if len(list2)>0:
                for item in list2:
                    list1.append(item)
            
            list1 = list(set(list1))
            list1.sort()
            #paragraph3 = doc.add_paragraph()
            paragraph3.add_run(ss.replace('\r', ''))
        print(p.stem)
        print(list1)
        if len(list1) >0:
            #paragraph3 = doc.add_paragraph()
            #paragraph3.add_run(ss.replace('\r', ''))
            doc.save(f'./templete/{p.stem}.docx')
            dict1 = {}
            tpl = DocxTemplate(f'./templete/{p.stem}.docx')
            for item in list1:
                dict1['img_'+str(item)] = InlineImage(tpl, image_descriptor=f'./img/{str(item)}.jpg',width=Mm(4))
            tpl.render(dict1)
            tpl.save(f'./docx/{p.stem}.docx')
In [2]:
import glob
import os,shutil
from pathlib import Path
import re
from docx import Document
import openpyxl
from docxtpl import DocxTemplate,InlineImage
from docx.shared import Mm

def find_all_numbers(text):
    return re.findall(r'\d+', text)

def replace_numbers(text):
    pattern = r'\d+'
    replacement = lambda x:'{{ '+ f'img_{x.group()}'+' }}'
    modified_text = re.sub(pattern, replacement, text)
    return modified_text

def replace_numbers_with_braces(text):
    # 使用正则表达式查找连续的数字
    pattern = re.compile(r'\d+')
    
    # 使用re.sub()进行替换
    result = pattern.sub(lambda x: f"{{{{ img_{x.group(0)} }}}}", text)
    
    return result
fn = '文本文件/槐荫堂7.txt'
with open(fn, "r") as f:
    list1 = []
   
    data = f.readlines()
    for s in data:
        list2 = []
        ss = replace_numbers_with_braces(s)
        list2 = find_all_numbers(s)
        if len(list2)>0:
            for item in list2:
                list1.append(item)
        
        list1 = list(set(list1))
        list1.sort()
        
    print(list1)
['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']
In [3]:
import openpyxl
from docxtpl import DocxTemplate,InlineImage
from docx.shared import Mm

dict1 = {}

tpl = DocxTemplate("templete/槐荫堂7.docx")
for item in list1:    
    dict1['img_'+str(item)] = InlineImage(tpl, image_descriptor=f'./img/黑先/{str(item)}.jpg',width=Mm(4))
#dict1['img_radar'] = InlineImage(tpl, image_descriptor=dict1['radar'])

tpl.render(dict1)
tpl.save('./docx/槐荫堂7(黑先).docx')
In [ ]:
import openpyxl
from docxtpl import DocxTemplate,InlineImage
from docx.shared import Mm

dict1 = {}

tpl = DocxTemplate("离垢居谈棋.docx")
for item in list1:    
    dict1['img_'+str(item)] = InlineImage(tpl, image_descriptor=f'./img/白先/{str(item)}.jpg',width=Mm(4))
#dict1['img_radar'] = InlineImage(tpl, image_descriptor=dict1['radar'])

tpl.render(dict1)
tpl.save('离垢居谈棋(白先).docx')

Excel文件内容生成markdown格式文本

In [ ]:
import openpyxl

fi_xls = 'data/对局目录.xlsx'

wb = openpyxl.load_workbook(fi_xls)
sheet = wb['卷18'] 


for n in range(sheet.max_row - 1,sheet.max_row+1):    
    print('## 第'+sheet.cell(n,1).value+'局  ')
    print(sheet.cell(n,2).value+'  ')
    print(sheet.cell(n,3).value+'  ')
    print('共'+sheet.cell(n,4).value+'着   ')
    if sheet.cell(n,5).value is not None:
        print(sheet.cell(n,5).value+'  ')
    print(sheet.cell(n,6).value+'  ')
    print('\n')
In [4]:
import openpyxl

fi_xls = 'data/对局目录.xlsx'

wb = openpyxl.load_workbook(fi_xls)
sheet = wb['卷24'] 


for n in range(sheet.max_row - 1,50):    
    print(sheet.cell(n,1).value+'  ')
    print(sheet.cell(n,2).value+'  ')
    print(sheet.cell(n,3).value+'  ')
    print(sheet.cell(n,4).value+'  ')
    
    print('\n')
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Cell In[4], line 5
      1 import openpyxl
      3 fi_xls = 'data/对局目录.xlsx'
----> 5 wb = openpyxl.load_workbook(fi_xls)
      6 sheet = wb['卷24'] 
      9 for n in range(sheet.max_row - 1,50):    

File ~/data/lib/python3.12/site-packages/openpyxl/reader/excel.py:344, in load_workbook(filename, read_only, keep_vba, data_only, keep_links, rich_text)
    314 def load_workbook(filename, read_only=False, keep_vba=KEEP_VBA,
    315                   data_only=False, keep_links=True, rich_text=False):
    316     """Open the given filename and return the workbook
    317 
    318     :param filename: the path to open or a file-like object
   (...)
    342 
    343     """
--> 344     reader = ExcelReader(filename, read_only, keep_vba,
    345                          data_only, keep_links, rich_text)
    346     reader.read()
    347     return reader.wb

File ~/data/lib/python3.12/site-packages/openpyxl/reader/excel.py:123, in ExcelReader.__init__(self, fn, read_only, keep_vba, data_only, keep_links, rich_text)
    121 def __init__(self, fn, read_only=False, keep_vba=KEEP_VBA,
    122              data_only=False, keep_links=True, rich_text=False):
--> 123     self.archive = _validate_archive(fn)
    124     self.valid_files = self.archive.namelist()
    125     self.read_only = read_only

File ~/data/lib/python3.12/site-packages/openpyxl/reader/excel.py:95, in _validate_archive(filename)
     88             msg = ('openpyxl does not support %s file format, '
     89                    'please check you can open '
     90                    'it with Excel first. '
     91                    'Supported formats are: %s') % (file_format,
     92                                                    ','.join(SUPPORTED_FORMATS))
     93         raise InvalidFileException(msg)
---> 95 archive = ZipFile(filename, 'r')
     96 return archive

File /usr/lib/python3.12/zipfile/__init__.py:1331, in ZipFile.__init__(self, file, mode, compression, allowZip64, compresslevel, strict_timestamps, metadata_encoding)
   1329 while True:
   1330     try:
-> 1331         self.fp = io.open(file, filemode)
   1332     except OSError:
   1333         if filemode in modeDict:

FileNotFoundError: [Errno 2] No such file or directory: 'data/对局目录.xlsx'
In [ ]: