Read/write files: excel, outlook, ppt, doc, pdf, xml, pptxml

Source: Internet
Author: User

Read/write files: excel, outlook, ppt, doc, pdf, xml, pptxml

Excel:

Read:

1 from openpyxl.reader.excel import load_workbook #Load excel load file
  2 
  3 wb = load_workbook (filename = self.path) # load
  4 if self.wb! = None:
  5 sheetnames = self.wb.get_sheet_names () # list
  6 if len (sheetnames)! = 0: #At least one sheet
  7 for sheet in sheetnames:
  8 ws = self.wb.get_sheet_by_name (sheet)
  9 for rline in range (1, ws.max_row + 1): # loop through each row
10 for column in range (1,1 + ws.max_column): # loop through each column
11 data = ws.cell (row = rline, column = column) .value
12 print (data, end = "")
13 print ("")
View Code

Create and write tables:

1 import win32com
  2 import win32com.client # control
  3
  4 def makeexcel (name):
  5 print (name)
  6 ex = win32com.client.Dispatch ("Excel.Application")
  7 wk = ex.Workbooks.Add () # Add a worksheet
  8 nowwk = wk.ActiveSheet # Focus table
  9 ex.Visible = True # can be seen
10
11 #Diagonal table add content
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) # save
17 wk.Close (True) # flase forcibly closes, True waits
18 ex.Application.Quit () # Exit system interface
19
20 names = ["A", "B", "C"]
21 for name in names:
22 makeexcel (name)
View Code

Outlook mail:

1 import win32com
  2 import win32com.client # control
  3
  4 def makemail (name):
  5 outlook = win32com.client.Dispatch ("Outlook.Application")
  6 mail = outlook.createItem (0) #First mail
  7 mail.Recipients.Add ("% s@163.com"% name)
  8 mail.Subject = u "Dear hello% s"% name
  9 mailtext = u "Dear Sensen"
10 mailtext + = u "My company has silicone dolls, which are cheap and look good. Welcome to experience"
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 # control
  3
  4 def makeppt (name):
  5 try:
  6 ppt = win32com.client.Dispatch ("PowerPoint.Application")
  7 pres = ppt.Presentations.Add () # One PPT multiple pages
  8 ppt.Visible = True
  9 
10 s1 = pres.Slides.Add (1,1)
11 s1_0 = s1.Shapes [0] .TextFrame.TextRange #Find the first text box
12 s1_0.Text = u "Dear% s Sensen \ n"% name
13
14 s1_1 = s1.Shapes [1] .TextFrame.TextRange # find the second text box
15 s1_1.InsertAfter (u "hello world")
16
17 filename = "C: \\ Users \\ Tsinghua-yincheng \\ Desktop \\" + name + ".ppt"
18 pres.SaveAs (filename) # save
19 pres.Close () # flase forcibly closes, True waits
20 ppt.Application.Quit () # Exit system interface
21 except AttributeError:
22 print ("error")
twenty three 
24 names = ["A", "B", "C"]
25 for name in names:
26 makeppt (name)
View Code

 

Doc:

Read:

1 import docx
2 
3 doc = docx.Document (filename) #Open the file based on the path
4 for para in doc.paragraphs: #loop every paragraph
5 print (para.text) # print text
View Code

Write:

1 import win32com
  2 import win32com.client # control
  3
  4 def makeword (name):
  5 print (name)
  6 word = win32com.client.Dispatch ("Word.Application") # OPERATION word
  7 doc = word.Documents.Add () # Insert documents
  8 word.Visible = True # can be seen
  9 
10 rng = doc.Range (0,0) #Operation position, starting from 00
11 rng.InsertAfter (u "Dear% s 先 森 \ n"% name)
12 rng.InsertAfter (u "I'm Mr. Z. I will be married to Yufeng on May 20th, 2017. I invite you to my wedding.
13 rng.InsertAfter (u "Best regards")
14
15 filename = "C: \\ Users \\ Tsinghua-yincheng \\ Desktop \\" + name + ". Doc"
16 doc.SaveAs (filename) #Save
17 doc.Close (True) #flase Forcibly closes, True waits
18 word.Application.Quit () # Exit system interface
19
20 names = ["A", "B", "C"]
21 for name in names:
22 makeword (name)
View Code

 

Pdf:

Read:

1 import PyPDF2
2 pdffile = open ("C: \\ Users \\ Tsinghua-yincheng \\ Desktop \\ Python Extension.pdf", "rb")
3 pdfreader = PyPDF2.PdfFileReader (pdffile) #Create reader
4 print (pdfreader.numPages) #Display the number of pages
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 #parse XML
 3
 4 #Open
 5 DomTree = xml.dom.minidom.parse (r "C: \ Users \ Tsinghua-yincheng \ Desktop \ 1.xml")
 6 collection = DomTree.documentElement # Load all elements
 7 if collection.hasAttribute ("shelf"): #Determine if the element has a shelf attribute,
 8 print (collection.getAttribute ("shelf")) #Display attribute -root
 9 movies = collection.getElementsByTagName ("movie")
10 print (movies)
11 for movie in movies: #Grabbed the list of the next layer
12 if movie.hasAttribute ("title"): # determine if the element has a shelf attribute,
13 print (movie.getAttribute ("title")) # show attribute -root
14
15
16 mytype = movie.getElementsByTagName ('type') [0] #Remove type
17 print ("---", mytype.childNodes [0] .data)
18
19 mytype1 = movie.getElementsByTagName ('format') [0] #Remove type
20 print ("---", mytype1.childNodes [0] .data)
twenty one 
22 mytype2 = movie.getElementsByTagName ('year') [0] #Remove type
23 if len (mytype2.childNodes) == 0:
24 pass
25 #print ("---", mytype2.childNodes [0] .data) 
View Code

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.