原创声明:本文为犇丿灬犇原创文章未经博主允许不得转载。犇丿灬犇博客地址 [犇丿灬犇博客]
接到亲戚的一个任务,读五年级的妹妹在学英语,要我给她的单词表加上音标,看了看任务量下了一跳,现在孩子五年级怎么就要学这么多单词,这要一个一个加得加到猴年马月上,刚好当时在学习python,心想用python编个程序会不会简单一些。
原文件是这样的:
程序代码
程序包括excel文件读写,单词音标库查询等。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import xlrd
from xlwt import *
import tkinter.filedialog
def create_dict():
d = dict() #建立一个空字典
f =open("dictionary.txt",mode="r",encoding="utf-16")#打开音标文件,注意读文件的格式
my_dict = f.readlines()#读出所有内容
my_dict.sort()#排序
f.close()#关闭
a=1
for line_1 in my_dict :#
line_1 = line_1.rstrip("\t\n")#
word_1,phonetic = line_1.split("\t",1)
word_1 = word_1.lstrip("\s")
word_1 = word_1.rstrip("\s")
phonetic = phonetic.lstrip("\s")
phonetic = phonetic.rstrip("\s")#去掉多余的空格和符号
if word_1 not in d:
d[word_1] = phonetic
else:
pass#不进行任何操作
return d
def read_xlsx():
filenames = tkinter.filedialog.askopenfilenames(filetypes = [('xls files', '.xls'),('xlsx files', '.xlsx'),('all files', '*')])
#print(filenames)
'''
string_filename = ""
for i in range(0, len(filenames)):
string_filename += str(filenames[i])
'''
workbook = xlrd.open_workbook(filenames[0])
booksheet = workbook.sheet_by_name('Sheet1')
p = list()
for row in range(booksheet.nrows):
row_data = []
for col in range(booksheet.ncols):
cel = booksheet.cell(row, col)
val = cel.value
try:
val = cel.value
#val = re.sub(r'\s+', '', val)
except:
pass
if type(val) == float:
val = int(val)
else:
val = str(val)
row_data.append(val)
p.append(row_data)
return p
def write_ex(table,i,j,det):
table.write(i, j, det)
table.col(j).width = 7000
def write_file(file,table,d,One_line,lin):
col=0
count=1
for word_2 in One_line:
if word_2!='':
if count%2:
if word_2 in d:
write_ex(table, lin, col, word_2)
write_ex(table, lin, col+1, d[word_2])
col=col+1
else:
write_ex(table, lin, col, word_2)
col = col + 1
else:
write_ex(table, lin, col, word_2)
col=col+1
count=count+1
if __name__ == '__main__':
dict=create_dict()
raw_data=read_xlsx()
file = Workbook(encoding='utf-8')
# 指定file以utf-8的格式打开
table = file.add_sheet('word')
lin=0
for One_line in raw_data:
write_file(file, table, dict, One_line, lin)
lin=lin+1
filenames = tkinter.filedialog.asksaveasfilename(title="保存", defaultextension=".xls", initialdir=".",
filetypes = [('xls files', '.xls'),
('xlsx files', '.xlsx'), ('all files', '*')],
initialfile="default1")
file.save(filenames)
将python文件编译生成EXE可执行文件
需要安装pyinstaller具体安装步骤见https://blog.csdn.net/u010104435/article/details/71435598
安装完成后执行pyinstaller -F -w文件名.py 语句即可。
效果
如要转载,请保持本文完整,并注明作者犇丿灬犇和本文原始地址: https://blog.csdn.net/qq_16307369/article/details/82222110
[1]https://blog.csdn.net/u010104435/article/details/71435598