Python3进行pdf文件分割及转word

今天有个pdf分割的需求,电脑装的Python3,网上查资料都是Python2的代码,所以整理一份3的

安装:

 pip install PyPDF2 
import PyPDF2

def funSplitPdf():
    pdf_file = open(
        '/path/fileName.pdf', 'rb')
    pdf_reader = PyPDF2.PdfReader(pdf_file)
    size = len(pdf_reader.pages)
    for i in range(size):
        out_pdf = PyPDF2.PdfWriter()
        out_pdf.add_page(pdf_reader.pages[i])
        out_pdf_file = open(
            '/path/fileName-'+str(i)+'.pdf',
            'wb')
        out_pdf.write(out_pdf_file)
        out_pdf.close()
if __name__ == '__main__':
    funSplitPdf()

pdf转word

安装:

 pip install pdf2docx 
import pdf2docx

def funPdf2Word():
    pdf2docx.parse('/path/fileName.pdf','/path/fileName.docx')

pdf转png

安装:

pip install  PyMuPDF
import fitz

def funPdf2Png():
    pdf_file = fitz.open('/path/fileName.pdf')
    for i in range(pdf_file.page_count):
        pdf = pdf_file[i]
        zoom_x = 1.3  # 水平方向
        zoom_y = 1.3  # 垂直方向
        mat = fitz.Matrix(zoom_x, zoom_y)
        pix = pdf.get_pixmap(matrix=mat)
        pix._writeIMG('/path/fileName-'+str(i)+'.png', 1, 1)
    pdf_file.close()

你可能感兴趣的:(pdf,python)