Python 取样式的内容 合并多个文件的样式 自定义样式

需求



需要处理的文件





更新

2020-10-9 16:10:16 样式加上了前缀

代码

import json
import os
import cv2
import time
import numpy as np
import win32gui, win32ui, win32con
from matplotlib import pyplot as plt
from ctypes import windll
from PIL import Image
from pymouse import PyMouse
from PIL import ImageGrab
import threading
import copy
import pyautogui
import re


def createFile(dirName):
    strPrefix = dirName+'_'# 前缀
    strStyles = ''
    pathDir = os.listdir(dirName)
    for index, value in enumerate(pathDir):
        print(index, value)
        with open(dirName+'/'+value, "r", encoding="utf-8") as f:
            fileName = value.replace(".cshtml", "")        
            line = f.read()
                        
            styles = re.findall(r'<style>(.*?)</style>', line, re.S|re.M)# 正则找style
            if len(styles)==0:
                continue# 没有匹配到就跳过
            style = styles[0]
            
            styles = re.findall(r'/*.*?*/', style, re.S|re.M)# 去注释
            for value in styles:
                style = style.replace(value,'')
    
            style = style.replace('}', '} .pages.'+strPrefix+fileName+' ')# 统一替换“}”
            
            style = style[0:style.rindex('}')+1]# 去末尾“}”
            style = ' .pages.'+strPrefix+fileName+' ' + style# 开头加样式
            
            strStyles = strStyles + style            
    file=open(dirName+'.txt', 'w', encoding='utf-8') 
    file.write(strStyles)
    file.close()

createFile('COneTable')
createFile('CTwoTable')




# 测试-去注释
# line='aa/*content:url803.png*/kk'
# styles = re.findall(r'/*.*?*/', line, re.S|re.M)# 正则找注释内容
# for value in styles:
    # line=line.replace(value,'')
# print(line)



原文地址:https://www.cnblogs.com/guxingy/p/13730480.html