python 版本: 2.7.16
2.x 版本自带md5
pip install pypinyin,xlwt, xlrd, xlutils.
md5
import md5
def main():
src = 'this is a md5 test.'
m1 = md5.new()
m1.update(src.encode(encoding='utf-8'))
print(m1.hexdigest())
pypinyin
from pypinyin import lazy_pinyin
def get_pinyin(data):
data = data.decode('utf-8') #解码utf-8
pinyin_list = lazy_pinyin(data) #可在data后面加参数, 不加默认无声调 例如:style=Style.TONE
ret = ''
for pinyin in pinyin_list:
ret += pinyin
return ret
xlwt
import xlwt
#设置表格样式
def set_style(name, bold = False, underline = False, italic = False):
style = xlwt.XFStyle() # 初始化样式
font = xlwt.Font() # 为样式创建字体
font.name = name # 字体
font.bold = bold # 加粗
font.underline = underline # 下划线
font.italic = italic # 斜体字
style.font = font # 设定样式
return style
def main():
# 创建一个workbook 设置编码
workbook = xlwt.Workbook(encoding = 'utf-8')
row = [111,222,333]
# 创建一个worksheet
worksheet = workbook.add_sheet('test')
for i in range(len(row)) :
worksheet.write(i, 0, row[i], set_style('Times New Roman'))
workbook.save('test.xls')
xlrd
import xlrd
def main():
workbook = xlrd.open_workbook('test.xls')
sheet_list = workbook.sheet_names() #获取全部sheet_name
main_sheet = workbook.sheet_by_index(0) #填入索引 和 填入名字 选一个
main_sheet = workbook.sheet_by_name(sheet_name) #填入已知sheet_name
nrows = main_sheet.nrows #获取列表的有效行数
ncols = main_sheet.ncols #获取列表的有效列数
for x in range(0, nrows): #从0开始至有效行数
row_data = main_sheet.row_values(x) #获取当前行的所有数据 例如 row_data = [1,2]
for y in range(0, ncols): #从0开始至有效列数
val = row_data[y] #获取当前行中列的数据 val = 1
xlutils
import xlwt
import xlrd
from xlutils.copy import copy
def main():
book = xlrd.open_workbook('test.xls')
workbook = copy(book) #将xlrd.Book转为xlwt.Workbook
worksheet = workbook.get_sheet('test') #获取sheet_name中的内容
worksheet.write(1, 0, 'aaa', set_style('Times New Roman')) #修改1行0列的参数, set_style参考 上文xlwt
workbook.save('test.xls')