zoukankan      html  css  js  c++  java
  • pdf及word文档的读取 pyPDF2,docx

    #!python3

    #-*- coding:utf8 -*-

     

    #PyPDF2可能会打不开某些pdf文档,也不能提取图片,图表或者其他媒介从PDF文件中。但是它能提取文本从PDF中,转化为字符。

    import PyPDF2 

    #以二进制方式 读模式打开一个pdf文件

    pdfFileObj=open('e:workdata_service.pdf','rb')

    #读取pdf文档

    pdfReader=PyPDF2.PdfFileReader(pdfFileObj)

    #返回的是pdf文档的总页数

    print(pdfReader.numPages)

    #获取单页的内容,页码数从0开始

    pageObj=pdfReader.getPage(0)

    #返回单页的文本内容

    pageObj.extractText()

    #对于有加密的pdf文档其读对象有属性 isEncrypted 

    print(pdfReader.isEncrypted) #若有加密,则属性值为True。直接获取某页的文本内容会报错。

    #通过方法decrypt()传递解密密码后可正常获取文本内容,密码以字符串形式传入。

    #pdfReader.decrypt('rosebud')

     

    #写pdf文档

     

    #创建pdf写对象

    pdfWriter=PyPDF2.PdfFileWriter()

     

    for pageNum in range(pdfReader.numPages):

        pageObj=pdfReader.getPage(pageNum)

        #向pdf写对象里添加内容(内容为pdf某个页面对象)

        pdfWriter.addPage(pageObj)

    #pdfWriter.addPage(pdfReader.getPage(3))

    #创建新的pdf文件

    pdfOutputFile=open('combinedminutes.pdf','wb')

    #将pdf写对象中的内容加进新建的pdf文件中

    #pdfWriter.write(pdfOutputFile) 此语句写入失败

    #写完后关闭pdf文件

    pdfOutputFile.close()

     

     

    #word文档读写

    import docx

    doc=docx.Document('C:\Users\li.wu\Desktop\有趣的植物.docx')

    #文档的总段落数

    print(len(doc.paragraphs))

    #可以一段一段的返回文本内容

    print(doc.paragraphs[0].text)

    #'第一章xa0xa0了解MySQL'

    #每个段落都有一个runs属性,runs的长度表示这个段落的格式的变化次数。

    len(doc.paragraphs[1].runs)

    #10

    #每个runs元素也都有一个text属性

    print(doc.paragraphs[1].runs[0].text) 

    #'xa0xa0xa0'

     

    #如果只关心文档中的内容,不在意格式的话,可以写一个函数直接获取整段文本:

    def getText(filename):

        doc=docx.Document(filename)

        fullText=[]

        for para in doc.paragraphs:

            fullText.append(para.text)

        return ' '.join(fullText)

    a=getText('e:workmysqll.docx')

    print(a.encode('utf-8'))

     

    '''

    默认的字体的类型有:

    'Normal' 'Heading5' 'ListBullet' 'ListParagraph' 'BodyText' 'Heading6' 'ListBullet2' 'MacroText' 'BodyText2' 'Heading7'

    'ListBullet3' 'NoSpacing' 'BodyText3' 'Heading8' 'ListContinue' 'Quote' 'Caption' 'Heading9' 'ListContinue2' 'Subtitle'

    'Heading1' 'IntenseQuote' 'ListContinue3' 'TOCHeading' 'Heading2' 'List' 'ListNumber' 'Title' 'Heading3' 'List2'

    'ListNumber2' 'Heading4' 'List3' 'ListNumber3'

    '''

     

    #有3种类型的风格,段落风格可以应用于Paragraph对象,个性风格可以应用于Run对象。关联风格可以应用于前两种风格。

    #设置风格属性时,风格类型名中间不要使用空格,例Subtle Emphasis ,入参应写成'SubtleEmphasis'

    #Paragraph和Run对象都有属相style,通过设置style的值可以设置段落和文字的格式。

    #当使用关联风格属性应用于Run对象时,需要在风格名后加上’Char'。例如要将Quote类的关联风格应用于Paragraph对象时,使用paragraphObj.style='Quote',

    #但是应用于Run对象时,应该使用runObj.style='QuoteChar'.

    #目前的Python-Docx(0.7.4)只支持默认的word风格。

     

    #run对象的text属性,每个text属相有三种值 True(打开) False(关闭) None(默认值)

    #run对象的text属性:

    '''

    bold            : The text appears in bold.

    italic          : The text appears in italic.

    underline       : The text is underlined.

    strike          : The text appears with strikethrough.

    double_strike   : The text appears with double strikethrough.

    all_caps        : The text appears in capital letters.

    small_caps      : The text appears in capital letters, with lowercase letters two points smaller.

    shadow          : The text appears with a shadow.

    outline         : The text appears outlined rather than solid.

    rtl             : The text is written right-to-left.

    imprint         : The text appears pressed into the page.

    emboss          : The text appears raised off the page in relief.

    '''

    doc.paragraphs[1].runs[0].underline=True

    doc.save('restyled.docx')

     

    #创建新的word文档

    doc=docx.Document()

    #添加段落

    doc.add_paragraph('Hello world!')

    a=doc.add_paragraph('this is a second paragraph')

    #在段落后添加语句

    a.add_run('This text is being addded to the second paragraph')

    #添加段落时可设置段落格式

    doc.add_paragraph('Hello world !','Title')

     

    #add_heading()方法是以某一种标题格式添加一个段落数据范围从0~4,0是主标题,4是第4级副标题。

    doc.add_heading('Header 0',0) #格式为标题1

    doc.add_heading('Header 1',1) #格式为标题2

    #通过在第一个末尾设置一个break,转到下一页

    doc.paragraphs[2].runs[0].add_break(docx.enum.text.WD_BREAK.PAGE)

    #Enumeration – WD_BREAK_TYPE

    '''

    WD_BREAK.LINE

    WD_BREAK.LINE_CLEAR_LEFT

    WD_BREAK.LINE_CLEAR_RIGHT

    WD_BREAK.TEXT_WRAPPING (e.g. LINE_CLEAR_ALL)

    WD_BREAK.PAGE

    WD_BREAK.COLUMN

    WD_BREAK.SECTION_NEXT_PAGE

    WD_BREAK.SECTION_CONTINUOUS

    WD_BREAK.SECTION_EVEN_PAGE

    WD_BREAK.SECTION_ODD_PAGE

    '''

    doc.add_paragraph('This is on the second page!')

     

    #添加图片到文档末尾(宽设置为4英寸,高设置为1厘米)高与宽可以不设置,则为常规大小。

    doc.add_picture('e:workcode.jpg',width=docx.shared.Inches(4),height=docx.shared.Cm(1))

     

    doc.save('e:workhelloworld.docx')

  • 相关阅读:
    [快捷键的使用] IntelliJ IDEA 将数据库里面的表转化为对象
    Mybatis事物浅谈
    CentOS7系统局域网内配置本地yum源解决cannot find a valid baseurl for repo
    CentOS6.5系统解决中文乱码问题
    tomcat启动报错:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
    zabbix3.4.8中提示host [4gronghe_110] not found
    CentOS7.6系统安装详解(含真机装系统的采坑之旅)!
    windows server 2012 R2修改默认远程端口
    两个div并排,右边div固定宽度,左边宽度自适应
    两个div并排,左边div固定宽度,右边宽度自适应
  • 原文地址:https://www.cnblogs.com/Ting-light/p/9548127.html
Copyright © 2011-2022 走看看