Python办公自动化【Word转换PDF、PDF读取内容、PDF合并文件、PDF拆分文件、PDF加密文件、PPT基本操作-增加幻灯片、增加内容】(六)-全面详解(学习总结---从入门到深化)(上)

简介: Python办公自动化【Word转换PDF、PDF读取内容、PDF合并文件、PDF拆分文件、PDF加密文件、PPT基本操作-增加幻灯片、增加内容】(六)-全面详解(学习总结---从入门到深化)

Python操作Word模块库文档



python文档 — python-docx 0.8.11 文档 (osgeo.cn)

python-docx — python-docx 0.8.11 documentation


Word转换PDF



使用office组件将word转换成PDF文件(缺点:只支持windows平台)


原理:使用python win32 库 调用word底层vba,将word转成pdf


安装


pip install pywin32


本地选装 office 套件,可以安装比较稳定的版本


常用方法与属性

函数名&属性&类  含义
win32com.client.gencache.EnsureDispatch()  设置word应用
ensureDispatch.Documents.Open(path)  读取数据
doc.ExportAsFixedFormat(pdfPath)  导出数据


代码

from win32com.client import gencache
from win32com.client import constants,gencache
def createPdf(wordPath, pdfPath):
  """
 word转pdf
 :param wordPath: word文件路径
 :param pdfPath: 生成pdf文件路径
 """
  word = gencache.EnsureDispatch('Word.Application')
  doc = word.Documents.Open(wordPath,ReadOnly=1)
  doc.ExportAsFixedFormat(pdfPath,
 constants.wdExportFormatPDF,
 Item=constants.wdExportDocumentWithMarkup,
 CreateBookmarks=constants.wdExportCreateHeadingBookmarks)
  word.Quit(constants.wdDoNotSaveChanges)
if __name__ == "__main__":
  # 路径填写绝对路径
  createPdf(
 r'D:\code\python\auto_code_word\base_data\原数据.docx',r'D:\code\python\auto_code_word\create_data
\10_word转换成pdf.pdf'
 )
  # 命令要安装pywin32模块,命令:pip install pywin32


PDF读取内容



安装

pip install pypdf2

pip install pdfplumber


常用方法与属性

函数名&属性&类  含义
PyPDF2.PdfFileReader(file)  读取PDF
pdf.getNumPages() 获取总页码
pdf.getPage(num)  获取第num页数据
page.extractText() 获取页面数据


代码

#pip install pypdf2
from PyPDF2 import PdfFileReader
def read_pdf1():
  # 打开文件
  with open('./base_data/10_word转换成pdf.pdf','rb') as f:
    # 将打开的文件传递给Reader对象
    reader = PdfFileReader(f)
    # 获取页面的总页数
    number = reader.getNumPages()
    print(number)
    # 获取单页
    page = reader.getPage(0)
    # 提取文本
    info = page.extractText()
    print(info)
# pip install pdfplumber
def read_pdf2():
  import pdfplumber
  # 打开文件
  with pdfplumber.open('./base_data/10_word转换成pdf.pdf') as f:
    # 获取数据
    print(f.pages)
    for p in f.pages:
      print(p.extract_text())
if __name__ =='__main__':
  # read_pdf1()
  read_pdf2()


PDF合并文件



在工作时,有时会碰到多个同类型内容的PDF文件,页码数又少,一个一个的打开又比较麻烦。这时应该如何处理呢?


这时就需要一个合并PDF的操作,而合并PDF操作的思路是:


1、读取源PDF文件

2、写入到一个新的PDF文件中


常用方法与属性

函数名&属性&类  含义
PyPDF2.PdfFileWriter()  创建一个pdf文件
write.addPage(page)  增加一页数据


代码

def merger_file(*m_path):
  from PyPDF2 import PdfFileReader,PdfFileWriter
  # 创建一个写入的对象
  writer = PdfFileWriter()
  # 读数据
  for path in m_path:
    # 创建reader
    reader = PdfFileReader(open(path,'rb'))
    # 读取页面数据
    for p in reader.pages:
      # 写新的pdf
      writer.addPage(p)
  # 保存
  with open('./create_data/02_合并pdf.pdf','wb') as f:
    writer.write(f)
if __name__ =='__main__':
  path = r'.\base_data\10_word转换成pdf.pdf'
  merger_file(path,path,path)


Python办公自动化【Word转换PDF、PDF读取内容、PDF合并文件、PDF拆分文件、PDF加密文件、PPT基本操作-增加幻灯片、增加内容】(六)-全面详解(学习总结---从入门到深化)(下):https://developer.aliyun.com/article/1420338

目录
相关文章
|
12天前
|
Python
python学习3-选择结构、bool值、pass语句
python学习3-选择结构、bool值、pass语句
|
2天前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
3天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
3天前
|
开发框架 前端开发 数据库
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
|
3天前
|
数据采集 数据可视化 数据处理
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
Python从入门到精通的文章3.3.1 深入学习Python库和框架:数据处理与可视化的利器
|
3天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
|
3天前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
21 0
|
10天前
|
机器学习/深度学习 算法 Python
使用Python实现集成学习算法:Bagging与Boosting
使用Python实现集成学习算法:Bagging与Boosting
19 0
|
10天前
|
BI 开发者 数据格式
Python代码填充数据到word模板中
【4月更文挑战第16天】
|
11天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
55 0