forwhat00  
 1 import openpyxl as xl
 2 from pathlib import Path
 3 obj=filepath=thefile=sheetlist=sheet=maxspcs=maxspc=0
 4 def chrlen(thechrs):
 5     count=0
 6     for i in thechrs:
 7         if(ord(i)>255):
 8             count+=2
 9         else:
10             count+=1
11     return count
12 def changejinzhi(thenum,letters="0123456789ABCDEF"):
13     if(thenum==0):return letters[0]
14     p=s=""
15     while(thenum>0):
16         jinzhi=len(letters)
17         p+=letters[thenum%jinzhi]
18         thenum=int(thenum/jinzhi)
19     for i in p:s=i+s
20     return s
21 
22 def loadfile():
23     global obj,filepath,thefile,sheetlist,sheet,maxspcs,maxspc
24     filepath=input("拖动文件到此处然后按下回车--")
25     thefile=Path(filepath)
26     reloadfile()
27 
28 def reloadfile(index=-1):
29     global obj,filepath,thefile,sheetlist,sheet,maxspcs,maxspc
30     obj=xl.load_workbook(thefile)
31     sheetlist=obj.worksheets
32     changesheet()
33 
34 def changesheet():
35     global obj,filepath,thefile,sheetlist,sheet,maxspcs,maxspc
36     for i in range(len(sheetlist)):
37         print(str(i)+"--"+sheetlist[i].title,end="\t")
38     print()
39     sheet=obj[sheetlist[int(input("序号--"))].title]
40     reloadsheet()
41 
42 def reloadsheet(ind=-1):
43     global obj,filepath,thefile,sheetlist,sheet,maxspcs,maxspc
44     maxspc=len(str(sheet.max_row))
45     maxspcs=[(max([chrlen(str(cell.value)) for cell in column])+3) for column in sheet.columns]
46 
47 def printsheet():
48     global obj,filepath,thefile,sheetlist,sheet,maxspcs,maxspc
49     print(" "*maxspc,end='\t')
50     for i in range(sheet.max_column):
51         thestr=changejinzhi(i,"ABCDEFGHIJKLMNOPQRSTUVWXYZ")
52         print(thestr,end=" "*(maxspcs[i]-chrlen(thestr)))
53     print()
54     rownum=0
55     for row in sheet.values:
56         rownum+=1
57         print(str(rownum)+" "*(maxspc-len(str(rownum)))+"|",end='  ')
58         for colmnnum in range(len(row)):
59             thestr=str(row[colmnnum])
60             print(thestr,end=' '*(maxspcs[colmnnum]-chrlen(thestr)))
61         print("|")
62 
63 def save():
64     global obj,filepath,thefile,sheetlist,sheet,maxspcs,maxspc
65     obj.save(filepath)
66 
67 helptext=\
68     "loadfile()-       重新选择文档\n"+\
69     "reloadfile()-     重新加载文档\n"+\
70     "changesheet()-    切换其他表格\n"+\
71     "reloadsheet()-    重新加载表格\n"+\
72     "printsheet()-     打印表格\n"+\
73     "save()-           保存当前表格\n"+\
74     "sheet['XN']='XXX' 修改XN为XXX 如:sheet['A1']='test'\n"+\
75     "改变表后记得save()\n"
76 loadfile()
77 printsheet()
78 while True:
79     stra=input(">->")
80     if(stra=="help"):print(helptext)
81     try:exec(stra)
82     except BaseException as reason:print(str(reason))

 

 

posted on 2021-03-08 23:13  forwhat00  阅读(11)  评论(0)    收藏  举报