读写文件:excel、outlook、ppt、doc、pdf、xml

excel:

读:

 1 from  openpyxl.reader.excel import load_workbook #载入excel加载文件
 2 
 3 wb = load_workbook(filename=self.path)  # 载入
 4 if  self.wb!=None:
 5     sheetnames = self.wb.get_sheet_names()  # list
 6     if len(sheetnames)!=0:#至少一个表格
 7          for  sheet  in  sheetnames:
 8                ws = self.wb.get_sheet_by_name(sheet)
 9                for rline in range(1, ws.max_row + 1):  # 循环每一行
10                     for column  in range(1,1+ws.max_column): # 循环每一列
11                          data=ws.cell(row=rline, column=column).value
12                          print(data,end="  ")
13                     print("")
View Code

建表并写入:

 1 import win32com
 2 import win32com.client#控制
 3 
 4 def makeexcel(name):
 5     print(name)
 6     ex=win32com.client.Dispatch("Excel.Application")
 7     wk=ex.Workbooks.Add()#增加一个工作表
 8     nowwk=wk.ActiveSheet# 焦点表格
 9     ex.Visible=True #可以看见
10 
11     #对角线表格添加内容
12     for  i in range(1,10):
13         nowwk.Cells(i,i).value="hello"+name
14 
15     filename = "C:\\Users\\Tsinghua-yincheng\\Desktop\\" + name + ".xls"
16     wk.SaveAs(filename)  # 保存
17     wk.Close(True)  # flase强行关闭,True等待
18     ex.Application.Quit()  # 退出系统接口
19 
20 names=["A","B","C"]
21 for name in names:
22     makeexcel(name)
View Code

outlook发邮件:

 1 import win32com
 2 import win32com.client#控制
 3 
 4 def  makemail(name):
 5     outlook=win32com.client.Dispatch("Outlook.Application")
 6     mail=outlook.createItem(0)#第一封邮件
 7     mail.Recipients.Add("%s@163.com"%name)
 8     mail.Subject=u"亲爱的你好%s"%name
 9     mailtext=u"尊敬的先森"
10     mailtext+=u"         我公司有硅胶娃娃,价格便宜,长得好看,欢迎体验"
11     mail.Body=mailtext
12     mail.Send()
13     outlook.Quit()
14 
15 names=["A","B","C"]
16 for name in names:
17     makemail(name)
View Code

PPT:

 1 import win32com
 2 import win32com.client#控制
 3 
 4 def makeppt(name):
 5     try:
 6         ppt=win32com.client.Dispatch("PowerPoint.Application")
 7         pres=ppt.Presentations.Add()#一个PPT多个页面
 8         ppt.Visible=True
 9 
10         s1=pres.Slides.Add(1,1)
11         s1_0=s1.Shapes[0].TextFrame.TextRange #找到第一个文本框
12         s1_0.Text=u"尊敬的%s先森\n"%name
13 
14         s1_1 = s1.Shapes[1].TextFrame.TextRange  # 找到第2个文本框
15         s1_1.InsertAfter(u"      hello world")
16 
17         filename = "C:\\Users\\Tsinghua-yincheng\\Desktop\\" + name + ".ppt"
18         pres.SaveAs(filename)  # 保存
19         pres.Close()  # flase强行关闭,True等待
20         ppt.Application.Quit()  # 退出系统接口
21     except AttributeError:
22         print("error")
23 
24 names=["A","B","C"]
25 for name in names:
26     makeppt(name)
View Code

 

doc:

读:

1 import   docx
2 
3 doc=docx.Document(filename)#根据路径打开文件
4 for para in doc.paragraphs:#循环每一个段落
5        print(para.text)#打印文本
View Code

写:

 1 import win32com
 2 import win32com.client#控制
 3 
 4 def makeword(name):
 5     print(name)
 6     word=win32com.client.Dispatch("Word.Application")#操作word
 7     doc=word.Documents.Add()#插入文档
 8     word.Visible=True#可以看见
 9 
10     rng=doc.Range(0,0) #操作位置,从00开始
11     rng.InsertAfter(u"尊敬的%s先森\n"%name)
12     rng.InsertAfter(u"     我是Z先生,定于2017.5.20与玉凤大婚,诚邀你来参加我的婚礼")
13     rng.InsertAfter(u"                      此致敬礼")
14 
15     filename="C:\\Users\\Tsinghua-yincheng\\Desktop\\"+name+".doc"
16     doc.SaveAs(filename)#保存
17     doc.Close(True)#flase强行关闭,True等待
18     word.Application.Quit()#退出系统接口
19 
20 names=["A","B","C"]
21 for name in names:
22     makeword(name)
View Code

 

pdf:

读:

1 import  PyPDF2
2 pdffile=open("C:\\Users\\Tsinghua-yincheng\\Desktop\\Python拓展.pdf","rb")
3 pdfreader=PyPDF2.PdfFileReader(pdffile)#创建阅读器
4 print(pdfreader.numPages)#显示页数
5 print(type(pdfreader.numPages))
6 for i  in range(pdfreader.numPages):
7     page=pdfreader.getPage(i)
8     print(page.extractText())
9     print("--------------------------------------------")
View Code

XML:

 1 from   xml.dom.minidom import  parse
 2 import  xml.dom.minidom  #解析XML
 3 
 4 #打开
 5 DomTree=xml.dom.minidom.parse(r"C:\Users\Tsinghua-yincheng\Desktop\1.xml")
 6 collection=DomTree.documentElement#加载所有元素
 7 if collection.hasAttribute("shelf"): #判断元素有没有shelf属性,
 8     print(collection.getAttribute("shelf"))#显示属性-root
 9 movies=collection.getElementsByTagName("movie")
10 print(movies)
11 for  movie  in  movies: #抓取了后一层列表
12     if movie.hasAttribute("title"):  # 判断元素有没有shelf属性,
13         print(movie.getAttribute("title"))  # 显示属性-root
14 
15 
16     mytype=movie.getElementsByTagName('type')[0]#取出type
17     print("---",mytype.childNodes[0].data)
18 
19     mytype1=movie.getElementsByTagName('format')[0]#取出type
20     print("---",mytype1.childNodes[0].data)
21 
22     mytype2=movie.getElementsByTagName('year')[0]#取出type
23     if len(mytype2.childNodes)==0:
24         pass
25     #print("---",mytype2.childNodes[0].data)
View Code

 

转载于:https://www.cnblogs.com/DBOOS/p/6917485.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值