python matplotlib阶段性总结——word转txt、绘图、文件操作
来源:互联网 发布:olay新生塑颜系列知乎 编辑:程序博客网 时间:2024/06/17 22:31
源自:http://blog.csdn.net/fortware/article/details/51935103
-
-
- import os
- import re
- import sys
- import chardet
- import fnmatch
- import win32com.client
- import numpy as np
- import matplotlib.pyplot as plt
-
- from matplotlib.font_manager import FontProperties
-
-
-
- PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
-
-
-
-
-
- first_f = 0
- first_month = 0
- first_year = 0
- file_cnt = 0
- all_money = 0
-
- week_money = [1342.8, 401.2, 158.0, 250.0, 870.0,
- 117.2, 860.4, 240.8, 283.3, 488.8,
- 442.5, 331.4, 337.12, 742.7, 638.2,
- 430.0, 253.2, 130.3, 614.5, 450.1,
- 198.8, 221.2, 324.9, 979.02, 170.8,
- 204.0, 560.3, 1106.3, 126.3, 639.6,
- 832.7, 631.0, 888.5, 952.7, 475.8,
- 751.0, 130.0, 459.1, 190.5, 1127.3,
- 308.5, 152.5, 844.0, 1394.4, 319.8,
- 1172.3, 789.51, 1277.3, 277.2, 742.3,
- 467.6, 580.7, 1263.4, 570.9, 381.5,
- 670.7, 607.5, 1219.0, 381.2, 398.0,
- 1132.5, 234.21, 701.4, 1160.1, 460.6,
- 353.4, 375.3, 137.0, 100.4, 724.2,
- 422.8, 684.4, 605.4, 679.3, 120.5,
- 159.5, 915.5, 965.5, 346.5, 254.5,
- 466.0, 1171.2, 190.0, 1075.7, 234.8,
- 198.79, 762.74, 332.57, 224.5, 207.0,
- 963.8, 750.44, 188.0, 624.1, 331.5,
- 473.1, 164.8, 207.5, 187.5, 135.5]
-
- new_month = [0]
- month_money = [0]
- basic_month = [u'2月',u'3月',u'4月',u'5月',
- u'6月',u'7月',u'8月',u'9月',
- u'10月',u'11月',u'12月']
-
-
-
-
- def money_plot():
-
- font = FontProperties(fname=r"C:\\WINDOWS\\Fonts\\simsun.ttc", size=10)
-
- plt.figure(figsize=(13,9.5))
- global week_money
- global month_money
- global new_month
- x1 = range(0,len(week_money))
- y1 = week_money
-
- x2 = range(0,len(week_money)%4==0 and len(week_money)/4 or len(week_money)/4+1)
- y2 = month_money
-
- plt.subplot(211)
- plt.plot(x1,y1,'r',label="money")
- plt.plot(x1,y1,'bo')
-
- plt.title(u"每周花费统计",fontproperties=font)
- plt.xlabel(u"周数",fontproperties=font)
- plt.ylabel(u"金额(¥)",fontproperties=font)
- plt.grid(True,color='g',linewidth=1)
- plt.legend()
-
- plt.subplot(212)
- plt.plot(x2, y2,'c',label="money")
- plt.plot(x2, y2,'bo')
-
- plt.xticks(x2,new_month,rotation=40,fontproperties=font)
-
- plt.title(u"每月花费统计",fontproperties=font)
- plt.xlabel(u"月份",fontproperties=font)
- plt.ylabel(u"金额(¥)",fontproperties=font)
- plt.grid(True,color='g',linewidth=1)
- plt.legend()
- plt.savefig("figure.png",format="png")
- plt.show()
-
-
-
-
-
-
- new_month = [0]
-
-
- def week_to_month():
-
- global week_money
- global month_money
- global first_month
- global first_year
- global new_month
- global basic_month
-
- sum = 0
- for i in range(0,len(week_money)):
- sum += week_money[i]
-
- if i%4 == 3:
- if month_money[0] == 0:
- month_money[0] = round(sum,2)
- else:
- month_money.append(round(sum,2))
- sum = 0
-
- if i == len(week_money)-1:
- if len(week_money)%4 != 0:
- month_money.append(round(sum,2))
- sum = 0
-
-
-
-
- index = first_month
- while index < len(month_money)+first_month:
- if new_month[0] == 0:
- if index == 1:
- new_month[0] = str(first_year)
- else:
- new_month[0] = basic_month[index-2]
- elif index%12 == 0:
- new_month.append(basic_month[12-2])
- elif index%12 == 1:
- new_month.append(str(first_year+index/12))
- else:
- new_month.append(basic_month[index%12-2])
- index += 1
-
-
-
-
-
-
-
- def read_txt_data():
-
- global week_money
- global all_money
- global file_cnt
- global first_f
- global first_month
- global first_year
-
- tmp_money = 0
- for root, dirs,files in os.walk(PATH):
- for _dir in dirs:
- pass
- for _file in files:
- if fnmatch.fnmatch(_file,'*.txt'):
- work_file = os.path.join(root, _file)
- f = open(work_file,'r')
- line = f.readline()
-
- print line
-
- if first_f == 0:
- first_f = 1
- date_data = re.findall(r"\d+\.?\d*",line)
- first_year = int(date_data[0])/10000
- first_month = int(date_data[0])%10000/100
-
- else:
- pass
- while True:
- line = f.readline()
- if line:
-
-
- content = line
- start = content.find("总计")
-
- if start != -1:
-
-
- num = re.findall(r"\d+\.?\d*",content)
- tmp_money = round(float(num[0]),2)
-
- if week_money[0] == 0:
- week_money[0] = tmp_money
- else:
- week_money.append(tmp_money)
- file_cnt += 1
- print "Your week money is",tmp_money
- all_money = all_money + tmp_money
- else:
- pass
- else:
- break
- f.close()
- print "\n","All used money is",all_money
- else:
- continue
- print "\n","eve_week_money:",week_money,"\n","total file:",file_cnt
-
-
-
-
-
- def word_to_txt():
- file_cnt = 0
- wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")
-
- print "Current path is "+PATH
-
- try:
-
- for root, dirs,files in os.walk(PATH):
- for _dir in dirs:
- pass
- for _file in files:
-
- if not fnmatch.fnmatch(_file,'*.doc'):
-
- continue
-
- word_file = os.path.join(root, _file)
- wordapp.Documents.Open(word_file)
-
- docastxt = word_file[:-3] +'txt'
-
- wordapp.ActiveDocument.SaveAs(docastxt,FileFormat = win32com.client.constants.wdFormatText)
- wordapp.ActiveDocument.Close()
-
- file_cnt += 1
- print "file %dth convert success ."%file_cnt
- finally:
- wordapp.Quit()
- print "All file convert success !"
-
-
- if __name__ == '__main__':
- try:
-
- read_txt_data()
- week_to_month()
- money_plot()
- except IOError, e:
- print e
-
绘图如下: