python 文本替换界面

import xlrd
import re


def save_lua():
    workbook = xlrd.open_workbook('code.xls')
    global mSheetsData
    mSheetsData = workbook.sheets()[0]
    global mExcelData
    mExcelData = []

    path_list = []
    init_data(path_list)

    for i in range(0, len(path_list)):
        print(path_list[i])
        write_text(path_list[i])


def init_data(path_list):
    for rx in range(0, mSheetsData.nrows):
        index = mSheetsData.cell(rx, 0).value
        ori = mSheetsData.cell(rx, 1).value
        des = mSheetsData.cell(rx, 2).value
        path = mSheetsData.cell(rx, 3).value
        temp_list = [index, ori, des, path]
        mExcelData.append(temp_list)
        if path not in path_list:
            path_list.append(path)


def write_text(lua_path):
    f = open(lua_path, 'r+', encoding='UTF-8')
    lua_text = f.read()
    lua_text = re.sub("GameCodeIndex\d+", replace_code, lua_text)
    f.seek(0)
    f.truncate()
    f.write(lua_text)
    f.close()


def replace_code(match):
    return find_des_by_index(match.group())


def find_des_by_index(index):
    for value in mExcelData:
        if str(value[0]) == str(index):
            if str(value[2]) != "default":
                return "xc.DBConstText.GetText({0})".format('"' + str(value[2]) + '"')
            else:
                return '"' + str(value[1]) + '"'

    return str(value[0])

 

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow
from map_window import Ui_MainWindow
from PyQt5.QtWidgets import QFileDialog
from excel2text import save_lua
from text2excel import save_excel
import os


class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.dir_path = ""

    def choose_path(self):
        self.dir_path = QFileDialog.getExistingDirectory(self, "choose directory", r"C:\Users\Administrator\Desktop")
        self.label_path.setText(self.dir_path)

    def lua2excel_fun(self):
        save_excel(self.dir_path)
        self.lua2excel_state.setText("success")

    def open_excel(self):
        path = os.getcwd()
        os.system('explorer.exe /n, ' + path)

    def excel2lua_fun(self):
        save_lua()
        self.excel2lua_state.setText("success")


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())

 

 

import xlwt
import re
import os

mRow = -1
mFilePath = ""


def save_excel(dir_path):
    global mRow
    mRow = -1

    global mWorkSheet

    work_book = xlwt.Workbook(encoding='ascii')
    mWorkSheet = work_book.add_sheet('text')

    file_list = []
    set_file_list(dir_path, file_list)

    for i in range(0, len(file_list)):
        path = file_list[i]
        if path[path.index('.') + 1:] == "lua":
            write_excel(file_list[i])

    work_book.save('code.xls')


def set_file_list(cur_path, file_list):
    cur_list = os.listdir(cur_path)
    for i in range(0, len(cur_list)):
        path = os.path.join(cur_path, cur_list[i])
        if os.path.isfile(path):
            file_list.append(path)
        else:
            set_file_list(path, file_list)


def write_excel(file_path):
    global mFilePath
    mFilePath = file_path
    f = open(file_path, 'r+', encoding='UTF-8')
    lua_text = f.read()
    lua_text = re.sub("\"[^\"\n\r]*[\u4e00-\u9fa5]+[^\"\r\n]*\"", replace, lua_text)
    f.seek(0)
    f.truncate()
    f.write(lua_text)
    f.close()


def replace(match):
    global mRow
    mRow = mRow + 1
    mWorkSheet.write(mRow, 0, label='GameCodeIndex' + str(mRow))
    mWorkSheet.write(mRow, 1, label=str(match.group())[1:-1])
    mWorkSheet.write(mRow, 2, label='default')
    mWorkSheet.write(mRow, 3, label=mFilePath)
    return 'GameCodeIndex' + str(mRow)
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>538</width>
    <height>215</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="choose_path_button">
    <property name="geometry">
     <rect>
      <x>440</x>
      <y>20</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>选择</string>
    </property>
   </widget>
   <widget class="QLabel" name="tip1">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>20</y>
      <width>151</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>1.选择需要转换的文件夹</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_path">
    <property name="geometry">
     <rect>
      <x>160</x>
      <y>20</y>
      <width>271</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>c:\</string>
    </property>
   </widget>
   <widget class="QLabel" name="tip2">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>50</y>
      <width>171</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>2.转换前先关闭code.xls</string>
    </property>
   </widget>
   <widget class="QPushButton" name="lua2excel_button">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>50</y>
      <width>121</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>开始转换lua-&gt;xls</string>
    </property>
   </widget>
   <widget class="QLabel" name="lua2excel_state">
    <property name="geometry">
     <rect>
      <x>300</x>
      <y>50</y>
      <width>71</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>state</string>
    </property>
   </widget>
   <widget class="QLabel" name="tip3">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>80</y>
      <width>261</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>3.打开code.xls修改里面的default</string>
    </property>
   </widget>
   <widget class="QLabel" name="tip4">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>110</y>
      <width>51</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>4.转换</string>
    </property>
   </widget>
   <widget class="QPushButton" name="excel2lua_button">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>110</y>
      <width>121</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>开始转换xls-&gt;lua</string>
    </property>
   </widget>
   <widget class="QLabel" name="tip5">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>140</y>
      <width>251</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>5.把code.xls的中文和英文拷贝到W文本.xlsx</string>
    </property>
   </widget>
   <widget class="QLabel" name="excel2lua_state">
    <property name="geometry">
     <rect>
      <x>190</x>
      <y>110</y>
      <width>71</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>state</string>
    </property>
   </widget>
   <widget class="QPushButton" name="open_excel_button">
    <property name="geometry">
     <rect>
      <x>230</x>
      <y>80</y>
      <width>75</width>
      <height>23</height>
     </rect>
    </property>
    <property name="text">
     <string>打开xls</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>538</width>
     <height>23</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>choose_path_button</sender>
   <signal>clicked()</signal>
   <receiver>MainWindow</receiver>
   <slot>choose_path()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>490</x>
     <y>65</y>
    </hint>
    <hint type="destinationlabel">
     <x>537</x>
     <y>132</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>lua2excel_button</sender>
   <signal>clicked()</signal>
   <receiver>MainWindow</receiver>
   <slot>lua2excel_fun()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>246</x>
     <y>95</y>
    </hint>
    <hint type="destinationlabel">
     <x>356</x>
     <y>172</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>excel2lua_button</sender>
   <signal>clicked()</signal>
   <receiver>MainWindow</receiver>
   <slot>excel2lua_fun()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>126</x>
     <y>155</y>
    </hint>
    <hint type="destinationlabel">
     <x>209</x>
     <y>214</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>open_excel_button</sender>
   <signal>clicked()</signal>
   <receiver>MainWindow</receiver>
   <slot>open_excel()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>288</x>
     <y>120</y>
    </hint>
    <hint type="destinationlabel">
     <x>329</x>
     <y>122</y>
    </hint>
   </hints>
  </connection>
 </connections>
 <slots>
  <slot>choose_path()</slot>
  <slot>lua2excel_fun()</slot>
  <slot>open_excel()</slot>
  <slot>excel2lua_fun()</slot>
 </slots>
</ui>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值