乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Python 進(jìn)階(七): Word 基本操作

       文炳春秋 2020-03-13

      1. 概述

      Word 是一個(gè)十分常用的文字處理工具,通常我們都是手動(dòng)來(lái)操作它,本節(jié)我們來(lái)看一下如何通過(guò) Python 來(lái)操作。

      Python 提供了 python-docx 庫(kù),該庫(kù)就是為 Word 文檔量身定制的,安裝使用 pip install python-docx 命令即可。

      2. 寫(xiě)入

      首先,我們使用 Python 來(lái)創(chuàng)建一個(gè) Word 文檔并向其中寫(xiě)入一些內(nèi)容。

      2.1 標(biāo)題

      我們先來(lái)創(chuàng)建 Word 文檔并向其中添加標(biāo)題,完整實(shí)現(xiàn)代碼如下所示:

      from docx import Document
      
      # 創(chuàng)建文檔
      document = Document()
      # 標(biāo)題
      document.add_heading('標(biāo)題0', 0)
      document.add_heading('標(biāo)題1', 1)
      document.add_heading('標(biāo)題2', 2)
      # 保存
      document.save('test.docx')
      

      看一下效果:

      2.2 段落

      我們接著向 Word 文檔中添加段落內(nèi)容,完整實(shí)現(xiàn)代碼如下所示:

      from docx import Document
      
      # 創(chuàng)建文檔
      document = Document()
      # 標(biāo)題
      document.add_heading('標(biāo)題0', 0)
      document.add_heading('標(biāo)題1', 1)
      # 段落
      document.add_paragraph('你們平時(shí)Word文檔用的多嗎?')
      # 列表
      document.add_paragraph('A:我們用的多', style='List Bullet')
      document.add_paragraph('B:我們用的少', style='List Bullet')
      document.add_paragraph('C:我們用的不多不少', style='List Bullet')
      document.add_heading('標(biāo)題2', 2)
      # 段落
      document.add_paragraph('我平時(shí)基本都是手動(dòng)操作Word文檔,現(xiàn)在打算利用Python來(lái)操作它,'
                             '你們平時(shí)是手動(dòng)操作Word文檔?如果是的話(huà),一起來(lái)了解下如何通過(guò)'
                             'Python來(lái)操作吧!')
      # 保存
      document.save('test.docx')
      

      看一下效果:

      2.3 表格

      我們接著向文檔中插入表格,完整實(shí)現(xiàn)代碼如下所示:

      from docx import Document
      
      # 創(chuàng)建文檔
      document = Document()
      # 標(biāo)題
      document.add_heading('標(biāo)題0', 0)
      document.add_heading('標(biāo)題1', 1)
      # 段落
      document.add_paragraph('你們平時(shí)Word文檔用的多嗎?')
      # 列表
      document.add_paragraph('A:我們用的多', style='List Bullet')
      document.add_paragraph('B:我們用的少', style='List Bullet')
      document.add_paragraph('C:我們用的不多不少', style='List Bullet')
      document.add_heading('標(biāo)題2', 2)
      # 段落
      document.add_paragraph('我平時(shí)基本都是手動(dòng)操作Word文檔,現(xiàn)在打算利用Python來(lái)操作它,'
                             '你們平時(shí)是手動(dòng)操作Word文檔?如果是的話(huà),一起來(lái)了解下如何通過(guò)'
                             'Python來(lái)操作吧!')
      # 表格
      table = document.add_table(rows=3, cols=2, style='Table Grid')
      # 表頭
      hc = table.rows[0].cells
      hc[0].text = '姓名'
      hc[1].text = '年齡'
      # 表體
      bc1 = table.rows[1].cells
      bc1[0].text = '張三'
      bc1[1].text = '22'
      bc2 = table.rows[2].cells
      bc2[0].text = '李四'
      bc2[1].text = '33'
      # 保存
      document.save('test.docx')
      

      看一下效果:

      2.4 圖片

      我們接著向文檔中插入圖片,完整實(shí)現(xiàn)代碼如下所示:

      from docx import Document
      from docx.shared import Inches
      
      # 創(chuàng)建文檔
      document = Document()
      # 標(biāo)題
      document.add_heading('標(biāo)題0', 0)
      document.add_heading('標(biāo)題1', 1)
      # 段落
      document.add_paragraph('你們平時(shí)Word文檔用的多嗎?')
      # 列表
      document.add_paragraph('A:我們用的多', style='List Bullet')
      document.add_paragraph('B:我們用的少', style='List Bullet')
      document.add_paragraph('C:我們用的不多不少', style='List Bullet')
      document.add_heading('標(biāo)題2', 2)
      # 段落
      document.add_paragraph('我平時(shí)基本都是手動(dòng)操作Word文檔,現(xiàn)在打算利用Python來(lái)操作它,'
                             '你們平時(shí)是手動(dòng)操作Word文檔?如果是的話(huà),一起來(lái)了解下如何通過(guò)'
                             'Python來(lái)操作吧!')
      # 表格
      table = document.add_table(rows=3, cols=2, style='Table Grid')
      # 表頭
      hc = table.rows[0].cells
      hc[0].text = '姓名'
      hc[1].text = '年齡'
      # 表體
      bc1 = table.rows[1].cells
      bc1[0].text = '張三'
      bc1[1].text = '22'
      bc2 = table.rows[2].cells
      bc2[0].text = '李四'
      bc2[1].text = '33'
      # 分頁(yè)
      # document.add_page_break()
      # 圖片
      document.add_picture('pic.jpg', width=Inches(1))
      # 保存
      document.save('test.docx')
      

      看一下效果:

      2.5 樣式

      我們?cè)僭O(shè)置一下基本樣式,比如:標(biāo)題居中、字體加粗、首行縮進(jìn)等,完整實(shí)現(xiàn)代碼如下所示:

      from docx import Document
      from docx.shared import Inches
      from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
      from docx.shared import Cm, Pt
      
      # 創(chuàng)建文檔
      document = Document()
      style = document.styles['Normal']
      # 標(biāo)題
      t0 = document.add_heading('標(biāo)題0', 0)
      # 居中
      t0.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
      document.add_heading('標(biāo)題1', 1)
      # 首行縮進(jìn)兩個(gè)字符
      paragraph_format = style.paragraph_format
      paragraph_format.first_line_indent = Cm(0.74)
      # 段落
      p1 = document.add_paragraph('你們平時(shí)')
      # 字體加粗
      p1.add_run('Word文檔').bold = True
      # 斜體
      p1.add_run('用的多嗎?').italic = True
      # 列表
      document.add_paragraph('A:我們用的多', style='List Bullet')
      document.add_paragraph('B:我們用的少', style='List Bullet')
      document.add_paragraph('C:我們用的不多不少', style='List Bullet')
      document.add_heading('標(biāo)題2', 2)
      # 段落
      p2 = document.add_paragraph('我平時(shí)基本都是手動(dòng)操作Word文檔,現(xiàn)在打算利用Python來(lái)操作它,'
                             '你們平時(shí)是手動(dòng)操作Word文檔?如果是的話(huà),')
      run = p2.add_run('一起來(lái)了解下如何通過(guò)Python來(lái)操作吧!')
      # 設(shè)置字體大小
      run.font.size = Pt(12)
      # 表格
      table = document.add_table(rows=3, cols=2, style='Table Grid')
      # 表頭
      hc = table.rows[0].cells
      hc[0].text = '姓名'
      hc[1].text = '年齡'
      # 表體
      bc1 = table.rows[1].cells
      bc1[0].text = '張三'
      bc1[1].text = '22'
      bc2 = table.rows[2].cells
      bc2[0].text = '李四'
      bc2[1].text = '33'
      # 分頁(yè)
      # document.add_page_break()
      # 圖片
      document.add_picture('pic.jpg', width=Inches(1))
      # 保存
      document.save('test.docx')
      

      看一下效果:

      3. 讀取

      我們?cè)賮?lái)讀取一下之前 Word 文檔中寫(xiě)入的內(nèi)容,完整代碼實(shí)現(xiàn)如下所示:

      from docx import Document
      
      # 打開(kāi)文檔
      document = Document('test.docx')
      # 讀取標(biāo)題、段落、列表內(nèi)容
      ps = [ paragraph.text for paragraph in document.paragraphs]
      for p in ps:
          print(p)
      # 讀取表格內(nèi)容
      ts = [table for table in document.tables]
      for t in ts:
          for row in t.rows:
              for cell in row.cells:
                  print(cell.text, end=' ')
              print()
      

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多