예제 #1
0
파일: xls2mdx.py 프로젝트: tuotech/mdx
def main():
    py_location = os.path.dirname(os.path.abspath(__file__))
    print(py_location)  # 打印当前 py 文件所在目录
    workbook = xlrd.open_workbook(os.path.join(py_location, r"通用规范汉字表.xls"))
    dictionary = {}
    for sheet in workbook.sheets():
        for row in range(sheet.nrows):
            zi_tou = sheet.cell(row, 1).value
            xu_hao = str(int(sheet.cell(row, 0).value)).zfill(4)
            print(sheet.name, xu_hao, zi_tou)
            #f.write(zi_tou + '\t' + xu_hao + '\t' + sheet.name + '\n')
            dictionary[
                zi_tou] = '<b>' + zi_tou + '</b>' + '\t' + xu_hao + '\t' + sheet.name

    writer = MDictWriter(dictionary, title="", description="")
    outfile = open(os.path.join(py_location, r"通用规范汉字表.mdx"), "wb")
    writer.write(outfile)
    outfile.close()

    print('done!')
예제 #2
0
파일: ZipMdict.py 프로젝트: jn7163/ZipMdict
def write_mdict():
    """
        Zip/Write button's command: zip/write .txt files into a file that ends with "mdx"/"mdd".
        (Adapted from "readmdict", cf. https://jingyan.baidu.com/article/95c9d20d47583bec4e756132.html)

        Returns:
            If successful, internal files of .mdx/.mdd will appear in a folder (named ""Unzipped_mdx)
                in the same folder as .mdx/.mdd, and an messagebox will pop up showing the number of entries in .mdx
                or the number of files in .mdd.
            If failed, an errorbox will pop up requesting you to specify a valid MDX/MDD file.

    """
    import io
    import sys
    import importlib
    importlib.reload(sys)
    import collections
    from collections import defaultdict
    from writemdict import MDictWriter, encrypt_key
    from ripemd128 import ripemd128

    # Modified by 掌上百科@maomaowei5655 in 2020-04-23:
    if write_path.split(os.path.sep)[-1].endswith('mdx'):
        # Zip files (dictionary_name.txt (and about.txt)) into .mdx
        # Delete .DS_Store in Mac:
        files_in_write_path = os.listdir(write_path)
        if '.DS_Store' in files_in_write_path:
            files_in_write_path.remove('.DS_Store')
        # The folder can only have at most 2 files:
        if len(files_in_write_path) > 2:
            tkinter.messagebox.showerror(
                'Error', "The number of files in the folder must be <=2.")
        elif len(files_in_write_path) == 0:
            tkinter.messagebox.showerror(
                'Error', "The number of files in the folder must be <=2.")
        elif len(files_in_write_path) == 1:
            # When the folder only has one .txt including entries
            global dictionary_txt
            dictionary_txt = files_in_write_path[0]

            head = 0
            new_mean = []
            f = io.open(os.path.join(write_path, dictionary_txt),
                        'r',
                        encoding='utf-8')
            d = defaultdict(list)  # 建立一个空字典,也可使用{}建立。
            for line in f:  # 每次从f中读入一行
                line = line.rstrip('\n')  # 去除行尾的换行符
                if line == '</>':
                    if head == 2:
                        new_mean[0:] = ["".join(new_mean[0:])]
                        d[word].append(new_mean[0])
                    head = 1
                    new_mean = []
                elif head == 1:
                    word = line
                    head = 2
                elif head == 2:
                    new_mean.append(line)
                    head = 2
            f.close()

            # Modified by 掌上百科@maomaowei5655 in 2020-04-23:
            if not os.path.exists(os.path.join(write_path, 'Zipped_mdx')):
                os.makedirs(os.path.join(write_path, 'Zipped_mdx'))
            outfile_mdx = open(
                os.path.join(write_path, 'Zipped_mdx',
                             dictionary_txt.split(os.path.extsep)[0] + '.mdx'),
                "wb")
            writer = MDictWriter(d,
                                 dictionary_txt.split(os.path.extsep)[0],
                                 dictionary_txt.split(os.path.extsep)[0])
            writer.write(outfile_mdx)
            outfile_mdx.close()
            # Modified by 掌上百科@maomaowei5655 in 2020-04-23:
            print('======== {} ========'.format(write_path))
            global write_number_report
            write_number_report = '{}.mdx'.format(
                dictionary_txt.split(os.path.extsep)[0])
            print('Output: {}'.format(write_number_report))
            # Modified by 掌上百科@maomaowei5655 in 2020-04-23:
            # region Messagebox for reporting outcome:
            tkinter.messagebox.showinfo(
                'Output', 'Write/Zip: \n\t{}\n\nOutput: \n\t{}'.format(
                    write_path, write_number_report))
            # endregion
        else:
            # When the folder have two .txt files including entries and about_information
            for file in files_in_write_path:
                if not file.startswith('about'):
                    dictionary_txt = file
                else:
                    global about_txt
                    about_txt = file

            head = 0
            new_mean = []
            f = io.open(os.path.join(write_path, dictionary_txt),
                        'r',
                        encoding='utf-8')
            d = defaultdict(list)  # 建立一个空字典,也可使用{}建立。
            for line in f:  # 每次从f中读入一行
                line = line.rstrip('\n')  # 去除行尾的换行符
                if line == '</>':
                    if head == 2:
                        new_mean[0:] = ["".join(new_mean[0:])]
                        d[word].append(new_mean[0])
                    head = 1
                    new_mean = []
                elif head == 1:
                    word = line
                    head = 2
                elif head == 2:
                    new_mean.append(line)
                    head = 2
            f.close()

            ff = io.open(os.path.join(write_path, about_txt),
                         'r',
                         encoding='utf-8')  # 词典about信息,txt文件请保存为utf-8
            about = []
            for line in ff:  # 每次从f中读入一行
                about.append(line)
            about[0:] = ["".join(about[0:])]

            # Modified by 掌上百科@maomaowei5655 in 2020-04-23:
            if not os.path.exists(os.path.join(write_path, 'Zipped_mdx')):
                os.makedirs(os.path.join(write_path, 'Zipped_mdx'))
            outfile_mdx = open(
                os.path.join(write_path, 'Zipped_mdx',
                             dictionary_txt.split(os.path.extsep)[0] + '.mdx'),
                "wb")
            writer = MDictWriter(d,
                                 dictionary_txt.split(os.path.extsep)[0],
                                 about[0])
            writer.write(outfile_mdx)
            outfile_mdx.close()
            # Modified by 掌上百科@maomaowei5655 in 2020-04-23:
            print('======== {} ========'.format(write_path))
            write_number_report = '{}.mdx'.format(
                dictionary_txt.split(os.path.extsep)[0])
            print('Output: {}'.format(write_number_report))
            # Modified by 掌上百科@maomaowei5655 in 2020-04-23:
            # region Messagebox for reporting outcome:
            tkinter.messagebox.showinfo(
                'Output', 'Write/Zip: \n\t{}\n\nOutput: \n\t{}'.format(
                    write_path, write_number_report))
            # endregion
    # elif write_path.split(os.path.sep)[-1].endswith('mdd'):
    # # Zip files in mdd
    # folder_name = re.search(r'^.*{}'.format(os.path.sep), write_path).group() + 'Zipped_mdd'
    # if not os.path.exists(folder_name):
    #     os.makedirs(folder_name)
    # outfile_name = os.path.join(folder_name, write_path.split(os.path.sep)[-1])
    # for file in os.listdir(write_path):
    #     if file.lower().endswith('png'or'gif'or'jpg'or'jpeg'or'css'):
    #         file_object = open(os.path.join(write_path, file), 'rb')
    #         file_contents = file_object.read()
    #         outfile_mdd = open(outfile_name, 'wb')
    #         d = {"\\{}".format(file) : file_contents}
    #         writer = MDictWriter(d, file.split('.')[0], file, is_mdd=True)
    #         writer.write(outfile_mdd)
    #         file_object.close()
    #         outfile_mdd.close()
    #     else:
    #         file_object = open(os.path.join(write_path, file), 'r', encoding='utf-8', errors='ignore')
    #         file_contents = file_object.read()
    #         outfile_mdd = open(outfile_name, 'w')
    #         d = {"\\{}".format(file): file_contents}
    #         writer = MDictWriter(d, file.split('.')[0], file, is_mdd=True)
    #         writer.write(outfile_mdd)
    #         file_object.close()
    #         outfile_mdd.close()
    # write_number_report = outfile_name
    # print('Output: {}'.format(write_number_report))
    # # region Messagebox for reporting outcome:
    # tkinter.messagebox.showinfo('Output', 'Write/Zip: \n\t{}\n\nOutput: \n\t{}'.format(write_path, write_number_report))
    # # endregion
    else:
        # Modified by 掌上百科@maomaowei5655 in 2020-04-23:
        print('Please put files in a folder suffixed mdx/mdd.')
        # region Messagebox for error:
        tkinter.messagebox.showerror('Error',
                                     "Please specify a valid MDX file.")
예제 #3
0
from writemdict import MDictWriter, encrypt_key
from ripemd128 import ripemd128
import io


# This is the dictionary we will use.
d = {
    "alpha":"<i>alpha</i>",
    "beta":"Letter <b>beta</b>",
    "gamma":"Capital version is Γ &lt;"}

### Example 1: Basic writing. All options default.
outfile = open("example_output/basic.mdx", "wb")
writer = MDictWriter(d, "Basic dictionary", "This is a basic test dictionary.")
writer.write(outfile)
outfile.close()


### Example 2: Demonstrates the use of UTF-16 encoding.
outfile = open("example_output/utf16.mdx", "wb")
writer = MDictWriter(d, 
                     "UTF-16 dictionary", 
                     "This is a test for the \"UTF-16\" encoding.",
                     encoding="utf-16")
writer.write(outfile)
outfile.close()

### Example 3: This is a test to create a UTF-16 dictionary containing characters outside the
#              Basic Multilingual Plane
d2 = {"𩷶":"A fish"}
예제 #4
0
from writemdict import MDictWriter, encrypt_key
from ripemd128 import ripemd128
import io

# This is the dictionary we will use.
d = {
    "alpha": "<i>alpha</i>",
    "beta": "Letter <b>beta</b>",
    "gamma": "Capital version is Γ &lt;"
}

### Example 1: Basic writing. All options default.
outfile = open("example_output/basic.mdx", "wb")
writer = MDictWriter(d, "Basic dictionary", "This is a basic test dictionary.")
writer.write(outfile)
outfile.close()

### Example 2: Demonstrates the use of UTF-16 encoding.
outfile = open("example_output/utf16.mdx", "wb")
writer = MDictWriter(d,
                     "UTF-16 dictionary",
                     "This is a test for the \"UTF-16\" encoding.",
                     encoding="utf-16")
writer.write(outfile)
outfile.close()

### Example 3: This is a test to create a UTF-16 dictionary containing characters outside the
#              Basic Multilingual Plane
d2 = {"𩷶": "A fish"}
outfile = open("example_output/utf16nonbmp.mdx", "wb")
예제 #5
0
from writemdict import MDictWriter, encrypt_key
from ripemd128 import ripemd128
import io


# This is the dictionary we will use.
d = {
    "alpha":"<i>alpha</i>",
    "beta":"Letter <b>beta</b>",
    "gamma":"Capital version is Γ &lt;"}

### Example 1: Basic writing. All options default.
outfile = open("example_output/basic.mdx", "wb")
writer = MDictWriter(d, "Basic dictionary", "This is a basic test dictionary.")
writer.write(outfile)
outfile.close()


### Example 2: Demonstrates the use of UTF-16 encoding.
outfile = open("example_output/utf16.mdx", "wb")
writer = MDictWriter(d, 
                     "UTF-16 dictionary", 
                     "This is a test for the \"UTF-16\" encoding.",
                     encoding="utf-16")
writer.write(outfile)
outfile.close()

### Example 3: This is a test to create a UTF-16 dictionary containing characters outside the
#              Basic Multilingual Plane
d2 = {"𩷶":"A fish"}
예제 #6
0
import io


# This is the dictionary we will use.
d = {
    "alpha":"<i>alpha</i>",
    "beta":"Letter <b>beta</b>",
    "gamma":"Capital version is Γ &lt;"}




### Example 1: Basic writing. All options default.
outfile = open("example_output/basic.mdx", "wb")
writer = MDictWriter(d, "Basic dictionary", "This is a basic test dictionary.")
writer.write(outfile)
outfile.close()


### Example 2: Demonstrates the use of UTF-16 encoding.
outfile = open("example_output/utf16.mdx", "wb")
writer = MDictWriter(d, 
                     "UTF-16 dictionary", 
                     "This is a test for the \"UTF-16\" encoding.",
                     encoding="utf-16")
writer.write(outfile)
outfile.close()

### Example 3: This is a test to create a UTF-16 dictionary containing characters outside the
#              Basic Multilingual Plane
d2 = {"𩷶":"A fish"}