第15章:处理PDF和Word文档(笔记)

本文介绍了如何使用Python处理PDF和Word文档,包括从PDF提取文本、解密PDF、创建PDF,以及读取、写入和操作Word文档,如添加标题、图像和样式。
摘要由CSDN通过智能技术生成

15.1:PDF文档

15.1.1:从PDF提取文本

import PyPDF2

pdfFileObj = open("meetingminutes.pdf", 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
print(pdfReader.numPages)

pageObj = pdfReader.getPage(0)
print(pageObj.extractText())

以二进制模式打开文件,然后将他传入PdfFileReader()中得到它的PdfFileReader对象

PdfFileReader的namePages属性:获取PDF共有几页

PdfFileReader的getPage()函数:获取传入页号中的页面信息(索引从0开始)

extractText()函数:放回改文本页的字符串。提取文本信息但并不完美

15.1.2:解密PDF

import PyPDF2

pdfReader = PyPDF2.PdfFileReader(open('encrypted.pdf', 'rb'))
print(pdfReader.isEncrypted)
print(pdfReader.decrypt('rosebud'))

PdfFileReader的isEncrypted属性:判断PDF是否加密,是返回True,否则False

PdfFileReader的decrypt()函数:传入口令字符串进行解密,如果加密文件没有调用这个函数在调用getPage()函数就会报错,并且该函数只解密了PdfFileReader对象,不是实际的PDF文档。

15.1.3:创建PDF

与PdfFileReader对象相对的PdfFileWriter对象,它可以创建一个新的PDF文档,但不能将任意的文本写入PDF,不像python可以写入纯文本文件,能力仅限于从其他PDF中复制页面、旋转页面、重叠页面和加密页面,不允许直接编辑PDF。

  1. 打开一个或多个已有的PDF,得到PdfFileReader对象
  2. 创建一个新的PdfFileWrite对象
  3. 将页面从PdfFileReader对象复制到PdfFileWrite对象中
  4. 利用PdfFileWrite对象写入输出的PDF

复制页面

import PyPDF2

pdf1File = open('meetingminutes.pdf', 'rb')
pdf2File = open('meetingminutes2.pdf', 'rb')
pdf1Reader = PyPDF2.PdfFileReader(pdf1File)
pdf2Reader = PyPDF2.PdfFileReader(pdf2File)
pdfWriter = PyPDF2.PdfFileWriter()

for pageNum in range(pdf1Reader.numPages):
    pdfObj = pdf1Reader.getPage(pageNum)
    pdfWriter.addPage(pdfOb
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public static int createdExcel(String PATH, List list, String title, String[] rowsName, String merged) { try { File myFile = new File(PATH); if (!myFile.exists()) { myFile.createNewFile(); } WritableWorkbook wbook = Workbook.createWorkbook(myFile); // 创建一个可写返回工作薄同给定文件名 WritableSheet wsheet = wbook.createSheet(title, 0); // sheet名称 // 设置字体 WritableFont wfont = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.BOLD); WritableCellFormat wcfFC = new WritableCellFormat(wfont); wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //单元格边缘线格式设置 wcfFC.setAlignment(jxl.format.Alignment.CENTRE); // 居中对齐 wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直居中 //wcfFC.setBackground(jxl.format.Colour.BLUE); // 蓝色底 // 设置行高和列宽 //wsheet.setColumnView(列数, 列宽); //wsheet.setRowView(行数, 行高); // 开始生成主体内容 for (int i = 0 ; i < rowsName.length; i++) { wsheet.addCell(new Label(i, 0, rowsName[i], wcfFC)); wsheet.setColumnView(i, 12); } wfont = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD); wcfFC = new WritableCellFormat(wfont); wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //单元格边缘线格式设置 //是数字时的格式化 // jxl.write.NumberFormat numberFormat = new jxl.write.NumberFormat(NumberFormat.CURRENCY_DOLLAR); // jxl.write.WritableCellFormat wcfFCNUMBER = new jxl.write.WritableCellFormat(wfont,numberFormat); // wcfFCNUMBER.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //单元格边缘线格式设置 // wcfFCNUMBER.setAlignment(jxl.format.Alignment.CENTRE); // 居中对齐 // wcfFCNUMBER.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直居中 //是数字时的格式化 wcfFC.setWrap(true); //合并单元格操作 //wsheet.mergeCells(坐标列1, 坐标行1, 坐标列2, 坐标行2) for (int i = 0; i < list.size(); i++) { String[] args = (String[]) list.get(i); for (int j = 0; j < args.length; j ++) { if (args[j].length()<15 ) { wsheet.addCell(new jxl.write.Number(j, i+1, Float.parseFloat(args[j]), wcfFC)); } else { wsheet.addCell(new Label(j, i+1, args[j], wcfFC)); } } if (merged.indexOf("," + (i+1) + ",") >=0) { wsheet.mergeCells(0, i+1, args.length-1, i+1); wsheet.setRowView(i, 1000); } //打印分页符 if (i % 20 == 0) { //wsheet.addRowPageBreak(i); } } // 主体内容生成结束 wbook.write(); // 写入文件 wbook.close(); return 1; } catch (Exception ex) { ex.printStackTrace(); return 0; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值