1. 使用Python创建PPT并写入内容
import os
import win32com
import win32com.client
def CreatePPTFile(FilePath):
PPT = win32com.client.Dispatch("PowerPoint.Application")
PPT.Visible = True
PPTFile = PPT.Presentations.Add()
PageOne = PPTFile.Slides.Add(1,1)
t1 = PageOne.Shapes[0].TextFrame.TextRange
t1.Text = "Sunck"
t2 = PageOne.Shapes[1].TextFrame.TextRange
t2.Text = "Sunck is a good man!"
PageTwo = PPTFile.Slides.Add(2,2)
t3 = PageTwo.Shapes[0].TextFrame.TextRange
t3.Text = "第二页标题"
t4 = PageTwo.Shapes[1].TextFrame.TextRange
t4.Text = "第二页第一栏内容"
PPTFile.SaveAs(FilePath)
PPTFile.Close()
PPT.Quit()
FilePath = r"\test.ppt"
CurrentPath = os.getcwd()
CreatePPTFile(CurrentPath+FilePath)
2. 使用Python创建Word并写入内容
import win32com
import win32com.client
import os
def CreateWordFile(FilePath,Name):
word = win32com.client.Dispatch("Word.Application")
word.Visible = True
doc = word.Documents.Add()
r = doc.Range(0,0)
r.InsertAfter("亲爱的" + Name + "\n")
r.InsertAfter(" 我想你。。。。\n")
doc.SaveAs(FilePath)
doc.Close()
word.Quit()
NameList = ['张三','李四','王五']
for Name in NameList:
Filepath = Name
CurrentPath = os.getcwd()
AbsolutePath = os.path.join(CurrentPath,Filepath)
CreateWordFile(AbsolutePath,Name)
3. 使用Python读取Word中的内容
import win32com
import win32com.client
import os
def ReadWordFile(FilePath,ToFilePath):
mw = win32com.client.Dispatch("Word.Application")
doc = mw.Documents.Open(FilePath)
doc.SaveAs(ToFilePath,2)
for paragraph in doc.Paragraphs:
line = paragraph.Range.Text
print(line)
doc.Close()
mw.Quit()
FilePath = r"\test.docx"
CurrentPath = os.getcwd()
AbsolutePath = CurrentPath + FilePath
ToFilePath = r"\test.txt"
AbsoluteToFilePath = CurrentPath + ToFilePath
ReadWordFile(AbsolutePath,AbsoluteToFilePath)