安裝 pip3 install python-docx
pip3 install python-docx -i https://pypi.douban.com/simple 豆瓣鏡像下載
內聯對象一般包括:段落(paragraph)、圖片(inline picture)、表(table)、標題(heading)、有序列表(numbered lists)、無序列表(bullets lists)
創建文檔
from docx import Document from docx.shared import Inches document = Document() #創建基於默認“模板”的空白文檔
打開文檔
document = Document('d:/test.docx') #打開文檔
添加段落
paragraph = document.add_paragraph('段落1') #在尾部添加段落 #參數 段落文本
在段落尾部添加文本
kuai=paragraph.add_run('我是中國人') #在段落尾部添加文本
#返回值:內聯對象
paragraphs=document.paragraphs #返回段落引用集合--列表 paragraphs[1].text="小Z同學:" #設置序號1段落的文本
返回段落集合
s=document.paragraphs #返回段落引用集合--列表
返回段落總數
s=len(document.paragraphs) #返回段落總數
返回指定段落的文本
s=document.paragraphs[0].text #返回指定段落的文本
設置段落樣式
paragraph.style = 'List Bullet' #設置段落樣式 paragraph =document.add_paragraph('段落4',style = 'List Bullet') #添加段落--帶段落樣式
返回段落樣式
s=document.paragraphs #返回段落引用集合--列表 s1=s[0].style #返回序號0段落的樣式 print(s1)
段落對齊
需要 from docx.enum.text import WD_ALIGN_PARAGRAPH
paragraph_format = paragraph.paragraph_format #創建段落格式對象 paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER #段落居中對齊 paragraph_format.alignment =WD_ALIGN_PARAGRAPH.LEFT #段落左對齊 paragraph_format.alignment =WD_ALIGN_PARAGRAPH.RIGHT #段落右對齊 paragraph_format.alignment =WD_ALIGN_PARAGRAPH.JUSTIFY #段落兩端對齊
paragraphs=document.paragraphs #返回段落引用集合--列表 paragraphs[4].paragraph_format.alignment=WD_ALIGN_PARAGRAPH.RIGHT #序號4段落右對齊
段落縮進
段落可以在左側和右側分別縮進。第一行也可以具有與段落其余部分不同的縮進,縮進的第一行有一個懸掛縮進
paragraph_format = paragraph.paragraph_format #創建段落格式對象 paragraph_format.left_indent = Inches(0.5) #段落左縮進0.5英寸 #需要 from docx.shared import Inches paragraph_format.right_indent = Pt(20) #右縮進20點 #from docx.shared import Pt paragraph_format.first_line_indent = Inches(0.5) #第一行縮進
paragraphs=document.paragraphs #返回段落引用集合--列表
paragraphs[2].paragraph_format.first_line_indent=Cm(0.74) #序號2段落首行縮進0.74厘米
#from docx.shared import Cm
段落間距
paragraph_format = paragraph.paragraph_format #創建段落格式對象 paragraph_format.space_before = Pt(38) #設置段落前間距 paragraph_format.space_after = Pt(19) #設置段落后間距
行間距
paragraph_format = paragraph.paragraph_format #創建段落格式對象 paragraph_format.line_spacing = Pt(50) #設置行間距
行距可以通過段落paragraph_format屬性的line_spacing或line_spacing_rule屬性來指定,當line_spacing設置為長度值時表示絕對距離,設置為浮點數時表示行高的倍數,設置為None表示根據繼承層次決定
保存文檔
document.save('d:/test.docx') #保存文檔--覆蓋原文檔
添加標題
document.add_heading('標題', level=0) #添加標題 #參數2 標題級別 0--9
添加分頁
document.add_page_break() #添加分頁
換頁方式
換頁方式決定一個段落在一個頁面結束附近如何表現,常用屬性有如下,每個屬性的取值可以為True、False、None:
- keep_together設置為True時使得整個段落出現在同一頁中,如果一個段落在換頁時可能會被打斷就在段前換頁;
- keep_with_next設置為True時使得本段與下一段出現在同一頁中;
- page_break_before設置為True時使得本段出現在新的一頁的頂端,例如新的一章標題必須從新的一頁開始;
- window_control設置為True時表示可以在必要的時候進行分頁,避免本段的第一行或最后一行單獨出現在一頁中
粗體和斜體
kuai=paragraph.add_run('我是中國人') #在段落尾部添加文本 #返回值:內聯對象 kuai.bold = True #給內聯設置粗體
kuai=paragraph.add_run('我是中國人') #在段落尾部添加文本 #返回值:內聯對象 kuai.italic = True #給內聯設置斜體
kuai.underline = True #給內聯設置下划線
字符格式
Run屬於行內元素的一種,是一個塊級元素的組成部分,可以看做是一段連續的具有相同格式(字體、字號、顏色、加粗、斜體、下畫線、陰影等)的文本。一般來說,一個段落會包含一個或多個Run,使得同一個段落中可以包含不同格式的文本
可以通過一個Run對象的font屬性來獲取和設置該Run的字符格式,例如字體名稱font.name、字體大小font.size、是否加粗font.bold、是否斜體font.italic、下畫線格式font.underline(True表示單下畫線,False表示沒有下畫線,或者使用WD_UNDERLINE中的成員設置更多下畫線格式)、字體顏色font.color.rgb(設置為docx.shared.RGBColor對象)
包括字體字體和大小,粗體,斜體和下划線
#設置字體--麻煩一點
kuai.font.name=u'華文彩雲'
r = kuai._element
r.rPr.rFonts.set(qn('w:eastAsia'), '華文彩雲')
#需要 from docx.oxml.ns import qn
kuai.font.size = Pt(30) #字體大小
kuai.font.color.rgb = RGBColor(0x42, 0x24, 0xE9) #設置字體顏色
#需要 from docx.shared import RGBColor
樣式
s=document.styles #獲取word所有樣式集合對象 for i in s: print(i)
章節
from docx import Document from docx.shared import Inches from docx.enum.section import WD_ORIENT, WD_SECTION document = Document() paragraph = document.add_paragraph('段落1') paragraph = document.add_paragraph('段落5:床前明月光,疑是地上霜。舉頭望明月,低頭思故鄉。') document.add_section() #添加新章節 paragraph = document.add_paragraph('章節2-1') document.add_section() #添加新章節 paragraph = document.add_paragraph('章節3-1') sections = document.sections #返回所有章節引用的對象 s=len(sections) #返回章節總數 section = sections[0] #返回指定章節的對象 section = document.sections[-1] # 返回文檔最后一個章節 new_height= section.page_height #返回章節頁面的高 #10058400 單位:像素 1英寸=914400像素 new_width=section.page_width #返回章節頁面的寬 #7772400 section.page_height=10058400 #設置章節的高度 section.page_width =4072400 #設置章節寬度 section.orientation = WD_ORIENT.LANDSCAPE #設置頁面方向 ??? #需要 from docx.enum.section import WD_ORIENT, WD_SECTION s=section.left_margin #返回左邊距--單位像素 s=section.right_margin #返回右邊距--單位像素 s=section.top_margin #返回上邊距--單位像素 s=section.bottom_margin #返回下邊距--單位像素 section.left_margin = Inches(1.5) #設置左邊距 s=section.header_distance #返回頁眉距離--單位像素 s=section.footer_distance #返回頁腳距離--單位像素 print(s) document.save('d:/test.docx')
頁眉和頁腳
Word支持頁眉和頁腳。頁眉是出現在每個頁面的上邊距區域中的文本,與文本主體分開,並且通常傳達上下文信息,例如文檔標題,作者,創建日期或頁碼。文檔中的頁眉在頁面之間是相同的,內容上只有很小的差異,例如更改部分標題或頁碼。頁眉也稱為運行頭
頁腳在頁眉的每個方面都類似,只不過它出現在頁面底部。它不應與腳注混淆,腳注在頁面之間內容是不一致的
頁眉和頁腳與一個章節相關聯,這允許每個章節具有不同的頁眉和/或頁腳
頁眉:
每個section對象都有一個.header屬性,可以訪問該節的_Header
對象:
from docx import Document document = Document() paragraph = document.add_paragraph('段落1') document.add_section() #添加新章節 paragraph = document.add_paragraph('段落2') document.add_section() paragraph = document.add_paragraph('段落3') section = document.sections[0] #返回序號0章節的引用 header = section.header #返回章節header引用 print(header.is_linked_to_previous) #章節頭(頁眉)是否無定義 #值為True表示_Header對象不包含章節頭定義,該章節將顯示與上一節相同的章節頭 #添加頁眉 paragraph = header.paragraphs[0] #返回頁眉序號0的段落的引用 #章節頭已包含單個(空)段落 #此時把header.is_linked_to_previous屬性設為false paragraph.text = "這是頁眉1" print(header.is_linked_to_previous) document.save('d:/test.docx')
刪除頁眉
通過將True分配給其.is_linked_to_previous
屬性,可以刪除不需要的頁眉:
header = section.header #返回章節header引用 header.is_linked_to_previous = True #刪除頁眉
頁腳:
每個section對象都有一個footer屬性,可以訪問該章節的頁腳對象:
section = document.sections[0] #返回序號0章節的引用 footer=section.footer #返回章節頁腳的引用 print(footer.is_linked_to_previous) #值為True表示頁腳對象不包含定義,該章節將顯示與上一節相同的頁腳 #添加頁腳 paragraph = footer.paragraphs[0] #返回頁腳序號0的段落的引用 #頁腳已包含單個(空)段落 #此時把footer.is_linked_to_previous屬性設為false paragraph.text = "這是頁腳1"
footer.is_linked_to_previous = True #刪除頁腳
制表符
tab_stops = paragraph.paragraph_format.tab_stops #返回段落格式制表符的引用 from docx.enum.text import WD_TAB_ALIGNMENT, WD_TAB_LEADER tab_stop = tab_stops.add_tab_stop(Inches(4.5), WD_TAB_ALIGNMENT.LEFT, WD_TAB_LEADER.DOTS) #添加制表位 #參數2 對齊--默認左 https://python-docx.readthedocs.io/en/latest/api/enum/WdTabAlignment.html#wdtabalignment #參數3 填充符--默認空格 https://python-docx.readthedocs.io/en/latest/api/enum/WdTabLeader.html#wdtableader print(tab_stop.position) #返回制表位位置--單位像素 print(tab_stop.position.inches) #返回制表位位置--單位英寸 tab_stops[0] #返回序號0制表位的引用
表格
添加表格
table = document.add_table(rows=2, cols=2) #添加表格
給單元格賦值和讀取單元格文本
cell = table.cell(0, 1) #返回表格的單元格對象 cell.text = '0行1列' #給單元格賦值 s=cell.text #返回單元格文本
row = table.rows[1] #返回行對象 row.cells[0].text = '一行零列' #給行對象的第n個單元格賦值 s=row.cells[1].text #返回行對象的第n個單元格的文本
col = table.columns[1] #返回列對象 col.cells[0].text = '零行1列' #給列對象的第n個單元格賦值 s=col.cells[1].text #返回列對象的第n個單元格的文本
tables=document.tables #返回文檔所有表格引用集合--列表 tables[0].cell(1,0).text="貓糧1" #給序號0的表格指定單元格設置文本
總行數和總列數
s = len(table.rows) #返回表格的總行數 s = len(table.columns) #返回表格的總列數
圖片
添加圖片
document.add_picture('大象.png') #添加圖片--添加的圖像以原始大小顯示
document.add_picture('大象.png', width=Inches(1.0)) #添加圖片 #參數2 圖品寬度 -寬度和高度只指定一個,另一個按比例縮放 #Inches--單位是英寸
document.add_picture('大象.png', width=Cm(11.8))#添加圖片
#需要 from docx.shared import Cm
天子驕龍