华南俳烁实业有限公司

python

當(dāng)前位置:中華考試網(wǎng) >> python >> python教程 >> 文章內(nèi)容

編寫python3處理word文檔代碼,今日粉教程請查收

來源:中華考試網(wǎng)  [2020年12月1日]  【

  直接使用word文檔已經(jīng)難不倒大家了,有沒有想過用python構(gòu)建一個word文檔寫點文章呢?當(dāng)然這個文章的框架需要我們用代碼一點點的建立,在過程上有一點繁瑣,一下子看不懂的小伙伴可以把它拆分成幾個部分來看。下面就在python3處理word文檔的代碼給大家?guī)碇v解,還會有一些設(shè)置文章格式的技巧。

  一個Word文檔,主要由下面這些內(nèi)容元素構(gòu)成,每個元素都有對應(yīng)的方法處理:

  標題:add_heading()

  段落:add_paragraph()

  文本:add_run(),其返回對象支持設(shè)置文本屬性

  圖片:add_picture()

  表格:add_table()、add_row()、add_col()

  import pathlib

  from docx import Document

  from docx.shared import Inches, Pt

  from docx.oxml.ns import qn

  path = list(pathlib.Path.cwd().parents)[1].joinpath('data/automate/003word')

  out_path = path.joinpath('003word_create.docx')

  img_path = path.joinpath('dance.jpg')

  document = Document()

  document.add_heading('Python1024_自動生成標題', 0)

  document.add_heading('基本:文本', level=1)

  p = document.add_paragraph('測試文本\n測試內(nèi)容\n')

  p.add_run('粗體部分內(nèi)容\n').bold = True

  p.add_run('斜體部分\n').italic = True

  p.add_run('下劃線部分\n').underline = True

  p.add_run('字體設(shè)置\n').font.size = Pt(24)

  # 測試第三方字體

  x = p.add_run('三方字體測試\n')

  x.font.name = 'Source Han Sans CN' # 思源字體

  x.element.rPr.rFonts.set(qn('w:eastAsia'), 'Source Han Sans CN')

  # 段落和引用

  document.add_heading('標題一:段落', level=1)

  document.add_paragraph('引用塊', style='Intense Quote')

  document.add_heading('標題1.1、無序列表', level=2)

  opts = ['選項1','選項2', '選項3']

  # 無需列表

  for opt in opts:

  document.add_paragraph(opt, style='List Bullet')

  document.add_heading('標題1.2、有序列表', level=2)

  # 有序列表

  for opt in opts:

  document.add_paragraph(opt, style='List Number')

  document.add_heading('標題二:圖片', level=1)

  document.add_picture(str(img_path), width=Inches(5))

  document.add_page_break()

  document.add_heading('標題三:表格', level=1)

  records = (

  (1, '電風(fēng)扇', '無葉風(fēng)扇'),

  (2, '吹風(fēng)機', '離子風(fēng)機'),

  (3, 'Macbook pro', 'Apple macbook pro 15寸')

  )

  # 表格

  table = document.add_table(rows=1, cols=3)

  # 表頭

  hdr_cells = table.rows[0].cells

  hdr_cells[0].text = '數(shù)量'

  hdr_cells[1].text = 'ID'

  hdr_cells[2].text = '描述信息'

  # 表格數(shù)據(jù)

  for qty, cid, desc in records:

  row_cells = table.add_row().cells

  row_cells[0].text = str(qty)

  row_cells[1].text = cid

  row_cells[2].text = desc

  # 保存文檔

  document.save(out_path)

  設(shè)置段落樣式,如下:

  document.add_paragraph('這是一個樣式為 ListBullet 的段落', style='ListBullet')

  或

  paragraph = document.add_paragraph('這是一個樣式為 ListBullet 的段落')

  paragraph.style = 'List Bullet'

  設(shè)置段落間距

  分為 段前 和 段后 ,設(shè)置值用 Pt 單位是 磅 ,如下:

  paragraph_format.space_before = Pt(18)

  paragraph_format.space_after = Pt(12)

  設(shè)置段落行距

  當(dāng)行距為 最小值 和 固定值 時,設(shè)置值單位為 磅 ,需要用 Pt ;當(dāng)行距為 多倍行距 時,設(shè)置值為數(shù)值,如下:

  from docx.shared import Length

  #SINGLE => 單倍行距(默認)

  #ONE_POINT_FIVE => 1.5倍行距

  #DOUBLE2 => 倍行距

  #AT_LEAST => 最小值

  #EXACTLY => 固定值

  #MULTIPLE => 多倍行距

  paragraph.line_spacing_rule = WD_LINE_SPACING.EXACTLY #固定值

  paragraph_format.line_spacing = Pt(18) # 固定值18磅

  paragraph.line_spacing_rule = WD_LINE_SPACING.MULTIPLE #多倍行距

  paragraph_format.line_spacing = 1.75 # 1.75倍行間距

  這里設(shè)置word的幾個格式技巧就講完啦,有的小伙伴直呼困難。小編想說的是只有我們親自去參與框架的搭建,才能對于以后操作word有更好的理解。

  如果你現(xiàn)在想學(xué)習(xí)python,贏取高薪工作機會,非常簡單,填寫下面信息,學(xué)好python技術(shù)高薪工作機會唾手可得。

責(zé)編:fushihao
  • 會計考試
  • 建筑工程
  • 職業(yè)資格
  • 醫(yī)藥考試
  • 外語考試
  • 學(xué)歷考試
澳门| 铁力市| 临潭县| 吐鲁番市| 永胜县| 韶山市| 渑池县| 屯留县| 南投市| 南康市| 荆州市| 博野县| 无为县| 巢湖市| 盐津县| 崇州市| 鄂托克旗| 宜黄县| 闸北区| 定远县| 肇东市| 苍山县| 阜平县| 丽江市| 铅山县| 海城市| 赣榆县| 香港 | 长岭县| 剑河县| 蒙阴县| 库尔勒市| 武鸣县| 鄂托克旗| 合阳县| 南靖县| 礼泉县| 正定县| 德化县| 呼玛县| 青阳县|