python matplotlib阶段性总结——word转txt、绘图、文件操作
源自:http://blog.****.net/fortware/article/details/51935103
- # -*- coding: cp936 -*-
- 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 = [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():
- #使用windows系统自带字体
- font = FontProperties(fname=r"C:\\WINDOWS\\Fonts\\simsun.ttc", size=10)
- #设置图表1尺寸
- 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使用条件表达式,不能被4整除时长度要+1
- 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]
- #每4周计算一次月消费
- 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
- #print "\n\n",month_money,"\n","total len_month",len(month_money)
- #计算月份编号表
- 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]#偏移为2,固减去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
- #for i in range(len(new_month)):
- #print new_month[i].encode('utf8')
- """
- 提取文件中每周的花费并保存下来
- """
- def read_txt_data():
- #读取txt文件内容
- 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
- #print "year",first_year ,"month",first_month
- else:
- pass
- while True:
- line = f.readline()
- if line:
- #打印每一行的内容
- #print line,
- content = line
- start = content.find("总计")#注意读出来的文本编码方式
- #判断是否是"总计那一行"
- if start != -1:
- #print start
- #获取位置
- 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
- """
- 将word文档转化为txt文档
- """
- 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:
- #匹配doc文件
- if not fnmatch.fnmatch(_file,'*.doc'):
- #跳出循环语句
- continue
- #将目录和文件名拼接 例如:c:\smart\test.doc
- word_file = os.path.join(root, _file)
- wordapp.Documents.Open(word_file)
- #修改成txt文件名 word_file[:-3]表示取从开始到倒数第三个字符
- docastxt = word_file[:-3] +'txt'
- #转换成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:
- #word_to_txt()
- read_txt_data()
- week_to_month()
- money_plot()
- except IOError, e:
- print e
绘图如下: