The application of python self-study-class25-python in office

The application of python in office

1. Reading and writing Word
2. Reading and writing Excel
3. Reading and writing PDF
4. Reading and writing ppt
5. Mail generated by calling outlook

++++++++++++++++++++++++ +++++++++++++++++
1. Read and write word to
read word, and save it as a txt file, here is mainly to call system word to read, and then use doc.SaveAs to write txt and save

import win32com
import win32com.client
myword=win32com.client.Dispatch("Word.Application")#调用系统word来读取
path=r"D:\Python代码\class25\2.docx"  #处理doc与docx
doc=myword.Documents.Open(path)
doc.SaveAs(r"D:\Python代码\class25\2.txt",2) #编号为2保存为txt,SaveAs必须是绝对路径
doc.Close()#关闭
myword.Quit()#退出

Writing word mainly uses InsertAfter() to insert a string

import win32com
import win32com.client
def makeword(name):
    print(name)
    word=win32com.client.Dispatch("Word.Application")
    doc=word.Documents.Add()  #$插入文档
    word.Visible=True  #可见
    rng=doc.Range(0,0)
    rng.InsertAfter(u"尊敬的%s先生\n"%name)  #匹配字符串
    rng.InsertAfter(u"今年6月我考高考")  #匹配字符串
    filename="D:\\Python代码\\class26\\"+name+".doc"
    doc.SaveAs(filename)  #保存
    doc.Close(True)  #关闭
    word.Application.Quit()
names=["txm","tmy"]
for name in names:
    makeword(name)

2. Read and write excel

from collections import OrderedDict #不可变字典
from pyexcel_xls import get_data  #读取数据
from pyexcel_xls import save_data  #写入数据

def readxls():
    path=r"D:\Python代码\class25\5.xls"
    xlsdata=get_data(path)
    print(xlsdata)
    print(type(xlsdata))  #字典类型
    for sheet in xlsdata:  #读取每一个key-value
        print(sheet,":",xlsdata[sheet])
def writexls():
    path="myself.xls"
    data=OrderedDict()#字典
    sheet_1=[]  #表格
    sheet_2=[]
    row_1=["a","b","c"]
    row_2=[1,2,3]
    sheet_1.append(row_1)
    sheet_1.append(row_2)
    sheet_2.append(row_1)
    sheet_2.append(row_2)
    data.update({
    
    "ABC":sheet_1})
    data.update({
    
    "xyz":sheet_2})
    save_data(path,data)
#readxls()
writexls()

3. Read and write PDF
For PyPDF2 this library, it only supports English, so it can only read English, and the use effect is not good

import PyPDF2  #pdf库的缺陷,只能读取英文
pdffile=open(r"D:\我学你妹\学员app操作手册.pdf","rb")
pdfreader=PyPDF2.PdfFileReader(pdffile)#读取pdf文件
print(pdfreader.numPages)  #显示页数
for i in range(pdfreader.numPages):
    page=pdfreader.getPage(i)
    print(page.extractText())#抓取文本

Crawl webpage PDF:

import urllib.request  #打开网页
import pdfminer.pdfinterp  #管理pdf资源
import pdfminer.converter  #文本转换
import pdfminer.layout  #处理pdf布局
import io #输入输出

def readpdf(pdffile):
    rsmgr=pdfminer.pdfinterp.PDFResourceManager()  #资源管理器
    retstr=io.StringIO()#文本输出
    lap=pdfminer.layout.LAParams() #处理文件布局
    device=pdfminer.converter.TextConverter(rsmgr,retstr,laparams=lap)#文本提取工具
    pdfminer.pdfinterp.process_pdf(rsmgr,device,pdffile)#根据文件进行解析
    device.close()#关闭设备
    content=retstr.getvalue() #抓取文本
    retstr.close()  #关闭文本输出
    return content  #返回文本
#打开一个文件
pdffile=urllib.request.urlopen("http://pythonscraping.com/pages/warandpeace/chapter1.pdf")
outputstring=readpdf(pdffile)  #处理文本
print(outputstring)  #输出文本

4. Create and write ppt

import win32com
import win32com.client
def makeppt(name):
    try:
        ppt = win32com.client.Dispatch("PowerPoint.Application")
        pres = ppt.Presentation.Add()  # 增加一个页面
        ppt.Visible = True
        s1 = pres.Slides.Add(1, 1)  # 增加一个页面
        s1_0 = s1.Shapes[0].TextFrame.TextRange  # 找到第一个文本
        s1_0.Text = "你好呀%s" % name
        s1_1 = s1.Shapes[1].TextFrame.TextRange  # 找到第二个文本
        s1_1.Text = "hello"
        filename = "D:\\Python代码\\class26\\" + name + ".ppt"
        pres.SaveAs(filename)
        pres.Close(True)  # 关闭
        ppt.Application.Quit()  # 退出
    except AttributeError:
        pass

names=["txm","tmy"]
for name in names:
    makeppt(name)

5. Mail occurs when Outlook is called

import win32com
import win32com.client
def makemail(name):
    outlook=win32com.client.Dispatch("Outlook.Application")
    mail=outlook.createItem(0)#第一封邮件
    mail.Recipients.Add("%[email protected]"%name)
    mail.Subject=u"你好呀%s"%name   #标题
    mailtext=(u"你好呀%s"%name)
    mailtext+=u"我是最叼的"
    mail.Body=mailtext    #正文
    mail.Send()#发送
    outlook.Quit()
names=["txm","tmy"]
for name in names:
    makemail(name)

Guess you like

Origin blog.csdn.net/weixin_46837674/article/details/114237430