Python-docx
安裝庫:pip install python-docx
幫助文檔:https://python-docx.readthedocs.io/en/latest/index.html
例子:

from docx import Document from docx.shared import Inches document = Document() document.add_heading('Document Title', 0) p = document.add_paragraph('A plain paragraph having some ') p.add_run('bold').bold = True p.add_run(' and some ') p.add_run('italic.').italic = True document.add_heading('Heading, level 1', level=1) document.add_paragraph('Intense quote', style='Intense Quote') document.add_paragraph( 'first item in unordered list', style='List Bullet' ) document.add_paragraph( 'first item in ordered list', style='List Number' ) document.add_picture('monty-truth.png', width=Inches(1.25)) records = ( (3, '101', 'Spam'), (7, '422', 'Eggs'), (4, '631', 'Spam, spam, eggs, and spam') ) table = document.add_table(rows=1, cols=3) hdr_cells = table.rows[0].cells hdr_cells[0].text = 'Qty' hdr_cells[1].text = 'Id' hdr_cells[2].text = 'Desc' for qty, id, desc in records: row_cells = table.add_row().cells row_cells[0].text = str(qty) row_cells[1].text = id row_cells[2].text = desc document.add_page_break() document.save('demo.docx')
東湖日報腳本源碼

# -*- coding:utf-8 -*- # ====#====#====#====#====#====#====#====#==== # @Time : 2020/4/2 10:58 # @Author : Alex_Dong # @Email : 1220274707@qq.com # @HomePage:https://www.cnblogs.com/xied/ # @File : 東湖日報腳本.py # @Software: PyCharm # ====#====#====#====#====#====#====#====#==== from docx import Document from docx.enum.text import WD_ALIGN_PARAGRAPH,WD_LINE_SPACING from docx.shared import Pt,Inches from docx import section from docx.enum.section import WD_SECTION,WD_ORIENT from docx.shared import RGBColor import time import docx from docx.oxml.ns import qn # 實例化一個document對象 donghu_document = Document() # 調取sections對象 sections = donghu_document.sections for section in sections: pass # 方法二 對下一也新增 # section = donghu_document.add_section() # 對屬性的修改 #方法允許在文檔末尾啟動一個新節。調用此方法后添加的段落和表將出現在新的部分:(增加一頁) # current_section = donghu_document.sections[-1] # last section in document # new_section = donghu_document.add_section(WD_SECTION.ODD_PAGE) # 長寬高 section.orientation = WD_ORIENT.LANDSCAPE # 尺寸像素點 # section.page_width = 15544800 # section.page_hwidth = 15544800 section.page_width = Inches(16) section.page_height = Inches(12) # 設置分欄為2 section._sectPr.xpath('./w:cols')[0].set(qn('w:num'),'2') #設置頁邊距 section.left_margin = Inches(0.5) section.right_margin = Inches(0.5) section.top_margin= Inches(0.5) section.bottom_margin= Inches(0.5) # 時間模塊 Data_now1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) Data_now = time.strftime('%Y-%m-%d', time.localtime(time.time())) print('現在時間:',Data_now1) # 大標題一 head = donghu_document.add_heading(level=0)#添加一級標題 head_run = head.add_run('東湖水環境提升工程設計、施工總承包(EPC)\n') head_run = head.add_run('工 程 日 報\n') head_run = head.add_run(Data_now1) head_run.font.size = Pt(24) head.alignment = WD_ALIGN_PARAGRAPH.CENTER#居中 # 段落一 p = donghu_document.add_paragraph('一、本日工作進展') p.add_run('bold').bold = True p.add_run(' and some ') p.add_run('italic.').italic = True #插入圖片 # 添加圖片,設置圖片大小 donghu_document.add_picture(r".\pic\mabu1.jpg", height=Inches(2.25)) donghu_document.add_picture(r'.\pic\mabu2.jpg',height=Inches(2.25)) # width=Inches(2.25)--2.25英寸 # 換頁和換行--如果是換行的話就不需要使用docx.enum.text.WD_BREAK.PAGE參數 # donghu_document.paragraphs[0].runs[0].add_break(docx.enum.text.WD_BREAK.PAGE) # 段落二 p = donghu_document.add_paragraph('\n\n\n\n\n\n\n\n') p = donghu_document.add_paragraph('二、本日完成情況對比:') p = donghu_document.add_paragraph('\n\n\n\n\n\n\n\n') p.add_run('bold').bold = True p.add_run(' and some ') p.add_run('italic.').italic = True # 段落三 p = donghu_document.add_paragraph('三、明日施工計划') p.add_run('bold').bold = True p.add_run(' and some ') p.add_run('italic.').italic = True # A添加表格,填入表格內容 table = donghu_document.add_table(rows=2, cols=2) table.cell(0, 0).text = "cell_00" table.cell(0, 1).text = "cell_01" table.cell(1, 0).text = "cell_10" table.cell(1, 1).text = "cell_11" # B##############################創建表格########## table = donghu_document.add_table(rows=1, cols=9, style='Table Grid') # 創建帶邊框的表格 hdr_cells = table.rows[0].cells # 獲取第0行所有所有單元格 hdr_cells[0].text = '序號' hdr_cells[1].text = '部位名稱' hdr_cells[2].text = '單位' hdr_cells[3].text = '設計總量' hdr_cells[4].text = '本日計划' hdr_cells[5].text = '本日完成' hdr_cells[6].text = '累計完成比例' hdr_cells[7].text = '累計完成' hdr_cells[8].text = '累計總占比' # 添加三行數據 data_lines = 5 for i in range(data_lines): cells = table.add_row().cells cells[0].text = '這是第%s行' % i #序號 cells[1].text = '部位數據%s' % i #部位 cells[2].text = '單位%s' % i #單位 cells[4].text = '單位%s' % i #'本日計划' cells[5].text = '單位%s' % i #'本日完成' cells[6].text = '單位%s' % i #'累計完成比例' cells[7].text = '單位%s' % i #'累計完成' cells[8].text = '單位%s' % i #'累計總占比' #插入無框表格 p = donghu_document.add_paragraph('\n\n<這是無框表格>') rows = 2 cols = 4 table = donghu_document.add_table(rows=rows, cols=cols) val = 1 for i in range(rows): cells = table.rows[i].cells for j in range(cols): cells[j].text = str(val * 10) val += 1 # 最后簽名 p = donghu_document.add_paragraph('\n\n\n\n\n\n\n\n編制:馬梅 審核:舒飛超') donghu_fileName = '【%s】東湖水環境提升日報'%Data_now+'.docx' donghu_fileName = str(donghu_fileName) print(donghu_fileName,'已經成功生成!!!') #存儲文檔 donghu_document.save('.\word\%s' %donghu_fileName)
一.簡單的打開保存
from docx import Document
document = Document('existing-document-file.docx') #可以docx=None
document.save('new-file-name.docx')
打開
f = open('foobar.docx', 'rb')
document = Document(f)
f.close()
二.API Documentation文檔對象
python-docx的API旨在使簡單的事情變得簡單,同時允許通過適當的、增量的理解來實現更復雜的結果。只使用一個對象docx.api就可以創建一個基本文檔。打開文件時返回的文檔對象。docx.api上的方法。文檔允許將塊級對象添加到文檔的末尾。塊級對象包括段落、內聯圖片和表。標題、項目符號和編號列表只是應用了特定樣式的段落。這樣一個文件。
- Document objects文檔對象
- Document
Settings
objects 文檔設置對象 - Style-related objects 風格相關的對象
- Text-related objects 文本相關的對象
- Table objects 表對象
- Section objects 部分對象
- Shape-related objects Shape-related對象
- DrawingML objects DrawingML對象
- Shared classes 共享類
- Enumeration 枚舉
三.Working with Sections
Word支持section的概念,即文檔的一個部分,具有相同的頁面布局設置,如頁邊距和頁面方向。例如,這就是文檔在縱向布局中包含某些頁面而在橫向布局中包含其他頁面的方式。大多數Word文檔只有一個默認的部分,而且大多數文檔沒有理由改變默認的頁邊距或其他頁面布局。但是,當您確實需要更改頁面布局時,您需要了解各個部分才能完成。
1.獲取sections對象
2.添加新的部分
add_section()方法允許在文檔末尾啟動一個新節。調用此方法后添加的段落和表將出現在新的部分:current_section = document.sections[-1] # last section in document
new_section = document.add_section(WD_SECTION.ODD_PAGE)
new_section.start_type
3.Section properties屬性
Section對象有11個屬性,允許發現和指定頁面布局設置。
-
Section start type部分啟動類型
-
section.start_type起始類型new_page(2),然后 section.start_type = WD_SECTION.ODD_PAGE
-
ODD_PAGE (4)>>>>>>>start_type的值是WD_SECTION_START枚舉的成員。
-
-
Page dimensions and orientation頁面尺寸和方向
- Section中的三個屬性描述了頁面的大小和方向。這些可以一起使用,例如,改變一個截面的方向從縱向到橫向:
- 用到了:section.orientation, section.page_width, section.page_height
-
>>> section.orientation, section.page_width, section.page_height (PORTRAIT (0), 7772400, 10058400) # (Inches(8.5), Inches(11)) >>> new_width, new_height = section.page_height, section.page_width >>> section.orientation = WD_ORIENT.LANDSCAPE >>> section.page_width = new_width >>> section.page_height = new_height >>> section.orientation, section.page_width, section.page_height (LANDSCAPE (1), 10058400, 7772400)
-
Page margins頁邊距
Section上的七個屬性一起指定了決定文本在頁面上出現位置的各種邊緣間距:
#section.left_margin, section.right_margin
#section.top_margin, section.bottom_margin
#section.gutter
#section.header_distance, section.footer_distance
1 from docx.shared import Inches 2 #section.left_margin, section.right_margin 3 #section.top_margin, section.bottom_margin 4 #section.gutter 5 #section.header_distance, section.footer_distance 6 7 section.left_margin = Inches(1.5) 8 section.right_margin = Inches(1) 9 section.left_margin, section.right_margin
-
Working with Headers and Footers 處理頁眉和頁腳
Word支持頁眉和頁腳。頁眉是出現在每頁頂部空白區域的文本,與正文分離,通常傳遞上下文信息,如文檔標題、作者、創建日期或頁碼。文檔中的頁眉在頁與頁之間是相同的,只有內容上的細微差異,比如更改了節標題或頁碼。頁眉也稱為運行頭。頁腳在任何方面都類似於頁眉,但它出現在頁面的底部。它不應該被混淆
-
-
Accessing the header for a section訪問節的標頭
- 每個section對象都有一個.header屬性,為該section提供對_Header對象的訪問:
-
Adding a header (simple case)添加標題(簡單的情況)
-
paragraph = header.paragraphs[0]
-
paragraph.text = "Title of my document"
-
-
-
-
Adding “zoned” header content添加“分區”標題內容
-
帶有多個“區域”的頁眉通常是使用精心放置的制表符來完成的。中心對齊和右對齊的“區域”所需的制表符停止是Word中頁眉和頁腳樣式的一部分。如果您使用的是自定義模板而不是python-docx默認模板,那么在模板中定義該樣式可能是有意義的。插入的制表符(“\t”)用於分隔左、中、右對齊的標題內容:
-
-
- >>> paragraph = header.paragraphs[0]
- >>> paragraph.text = "Left Text\tCenter Text\tRight Text"
- >>> paragraph.style = document.styles["Header"]
-
-
-
Removing a header
-
header.is_linked_to_previous = True
-
python給word添加換行換頁符
- #換頁只需要把docx.enum.text.WD_BREAK.PAGE作為唯一的參數傳遞給add_break
- #如果是換行的話就不需要使用docx.enum.text.WD_BREAK.PAGE參數
- doc2.paragraphs[0].runs[0].add_break(docx.enum.text.WD_BREAK.PAGE)
-
四.Styles
Questions:
-
-
What is a style in Word?
-
Why doesn’t the style I applied show up?
-
-
Access a style
The Styles
object is also iterable. By using the identification properties on BaseStyle
, various subsets of the defined styles can be generated. For example, this code will produce a list of the defined paragraph styles:Styles對象也是可迭代的。通過使用BaseStyle上的標識屬性,可以生成定義的樣式的各種子集。例如,這段代碼將生成定義的段落樣式列表:

>>> from docx.enum.style import WD_STYLE_TYPE >>> styles = document.styles >>> paragraph_styles = [ ... s for s in styles if s.type == WD_STYLE_TYPE.PARAGRAPH ... ] >>> for style in paragraph_styles: ... print(style.name) ... Normal Body Text List Bullet
-
Apply a style應用一個樣式

>>> document = Document() >>> paragraph = document.add_paragraph() >>> paragraph.style <docx.styles.style._ParagraphStyle object at <0x11a7c4c50> >>> paragraph.style.name 'Normal' >>> paragraph.style = document.styles['Heading 1'] >>> paragraph.style.name 'Heading 1'
-
Add or delete a style
#通過指定一個唯一的名稱和樣式類型,可以將一個新樣式添加到文檔中: >>> from docx.enum.style import WD_STYLE_TYPE >>> styles = document.styles >>> style = styles.add_style('Citation', WD_STYLE_TYPE.PARAGRAPH) >>> style.name 'Citation' >>> style.type PARAGRAPH (1) #使用base_style屬性指定新樣式應該繼承的格式設置: >>> style.base_style None >>> style.base_style = styles['Normal'] >>> style.base_style <docx.styles.style._ParagraphStyle object at 0x10a7a9550> >>> style.base_style.name 'Normal' #一個樣式可以從文檔中刪除,只需調用它的delete()方法: >>> styles = document.styles >>> len(styles) 10 >>> styles['Citation'].delete() >>> len(styles) 9