#!/usr/bin/env python
# -*- coding:utf-8 -*-
import xlrd
import Tkinter as tk
from xlutils.copy import copy
from xlwt import Style
import xlwt
import re
class excelMain(object):
def __init__(self):
self.createGUI()
def createGUI(self):
window = tk.Tk()
window.title(" EXCEL-SUPPORTER")
window.geometry("1300x700")
self.var = tk.StringVar()
self.l = tk.Label(window, textvariable=self.var, fg=("white"), bg='black', font=('Arial', 12), width=70, height=10)
self.l.pack()
self.e1 = tk.Entry(window, font=('Arial', 14))
self.e2 = tk.Entry(window, font=('Arial', 14))
self.e3 = tk.Entry(window, font=('Arial', 14))
self.e1.pack()
self.e2.pack()
self.e3.pack()
self.ll = tk.Label(window, text="请输入文件名,定额编号 , 数量", bg='white', fg='black', font=('Arial', 12), width=30, height=2)
self.ll.pack()
b = tk.Button(window, text='查找', font=('Arial', 12), width=10, height=1, command=self.buttonReturn)
b.pack()
window.mainloop()
def buttonReturn(self):
e2 = re.sub("-", "", self.e2.get())
if e2.isalnum() == True:
self.readExcel()
else:
self.var.set("请检查编号中是否有换行或空格")
def writeExcel(self, styl=Style.default_style):
name = self.e1.get()
job = self.e2.get()
num = self.e3.get()
book = xlrd.open_workbook(str(name)+ ".xls", formatting_info=True)
wb = copy(book)
Data_sheet = book.sheet_by_name(u'表三丙')
cols = Data_sheet.col_values(1)
wb.get_sheet(5).write(4, int(cols.index(job)), str(num), styl)
wb.save(str(name+ ".xls"))
def readExcel(self):
name = self.e1.get()
book = xlrd.open_workbook(str(name)+ ".xls", formatting_info=True)
Data_sheet = book.sheet_by_name(u'表三丙')
cols = Data_sheet.col_values(1)
if self.e2.get() in cols:
style = xlwt.easyxf();
self.writeExcel(style)
self.var.set("修改成功")
else:
self.var.set("无")
excelMain()
报错为
C:\Python27\python.exe C:/Users/86138/Desktop/ex/main.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1547, in __call__
return self.func(*args)
File "C:/Users/86138/Desktop/ex/main.py", line 44, in buttonReturn
self.readExcel()
File "C:/Users/86138/Desktop/ex/main.py", line 66, in readExcel
self.writeExcel(style)
File "C:/Users/86138/Desktop/ex/main.py", line 57, in writeExcel
wb.save(str(name+ ".xls"))
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 710, in save
doc.save(filename_or_stream, self.get_biff_data())
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 667, in get_biff_data
before += self.__all_fonts_num_formats_xf_styles_rec()
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 570, in __all_fonts_num_formats_xf_styles_rec
return self.__styles.get_biff_data()
File "C:\Python27\lib\site-packages\xlwt\Style.py", line 185, in get_biff_data
result += self._all_num_formats()
File "C:\Python27\lib\site-packages\xlwt\Style.py", line 209, in _all_num_formats
result += NumberFormatRecord(fmtidx, fmtstr).get()
File "C:\Python27\lib\site-packages\xlwt\BIFFRecords.py", line 785, in __init__
ufmtstr = upack2(fmtstr)
File "C:\Python27\lib\site-packages\xlwt\UnicodeUtils.py", line 50, in upack2
us = unicode(s, encoding)
TypeError: coercing to Unicode: need string or buffer, NoneType found