python辦公之python編輯word
1 前言
在工作中時常會有繁重的文案工作,接觸了python 之后,就會覺得這個比較簡單了,python 操作word 和 excel 是比較常用的操作,相對比較簡單,在本文中,我們就以 python 操作 word 為例來介紹一些簡單的操作。
2 前提準備
2.1 python-docx 的安裝
需要操作的前提是下載 docx 相關的操作類庫 python-docx ,操作的環(huán)境和 IDE 環(huán)境如下所示
#使用的python 版本 python3.7.6 IDE pycharm2019 # 安裝命令 pip install python-docx # 查看安裝版本 pip list | grep python-docx
2.2 docx 文檔的結構說明
事先聲明一下,python 操作的word版本必須是 docx 的版本,doc 的文檔暫不支持。另外 docx 文檔也是一種 xml 的數據組織格式, 首先了解一下其格式情況,

在word文檔中,其主要結構如下所述:
- 1 每個document包含多個paragraph,每個paragraph有多個run,每個run包含有(text文本,font字體,color顏色,字號)
- 2 每個document包含多個tables,table中有多個rows,每個row包含多個cells,每個cell中包含多個paragraph。對于寫word表格不論是 head 還是paragraph 基本操作都是先添加對象,然后再添加run就好了
- 3 word表格的結構包含head標題、normal 正文、Caption表
3 具體使用
3.1 創(chuàng)建標題
# 創(chuàng)建一個document
document = Document()
# 創(chuàng)建一個標題 默認是一級標題
head = document.add_heading(level=4)
run = head.add_run("這是一個四級標題 this is a title")
# font.name 只能設置西文字體
run.font.name = 'Times New Roman'
# 中文字體需要使用這種方式設置
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')
# 設置大小為11磅
run.font.size = Pt(16)
# 段落字體顏色
run.font.color.rgb = RGBColor(128, 0, 128)
# 是否加粗
run.bold = False
# 是否斜體
run.italic = False3.2 創(chuàng)建段落
# 創(chuàng)建一個段落
ph = document.add_paragraph()
# 添加段落 段落間距段落前13磅 段落后13磅 行間距固定值18磅
ph.paragraph_format.space_before = Pt(13)
ph.paragraph_format.space_after = Pt(13)
ph.paragraph_format.line_spacing = Pt(18)
# 設置2.5倍行間距
ph.paragraph_format.line_spacing = 2.5
# 段落縮進 段落左縮進0.5英寸 left_indent right_indent
# p.paragraph_format.left_indent = Inches(0.5)
# 首行縮進 首行縮進0.9cm
ph.paragraph_format.first_line_indent = Cm(0.9)
# 段落左對齊
ph.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
run1 = ph.add_run("歷史上第一個兒子當皇帝,老爹還活著的,當屬劉太公,也就是劉邦的父親。劉邦建立漢朝,稱帝,"
"每天還去拜見劉太公,后來有大臣進言講,雖然劉太公貴為皇帝父親,但也為人臣,不應該由皇帝前去拜見。")
run1.font.size = Pt(12)
run1.font.color.rgb = RGBColor(128, 128, 128)
run1.font.name = 'Times New Roman'
run1._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')3.3 創(chuàng)建表格
# 創(chuàng)建一個表格 3行四列 也可以不設置
table = document.add_table(rows=1, cols=3)
# 自動調整表格
table.autofit = True
# 設置表格樣式
table.style = 'Table Grid'
# 表頭
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
# 準備數據
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
# 添加內容
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 = desc3.4 文檔保存
# 保存文檔 指定保存位置 document.save(r"demo_word.docx")
3.5 獲取文檔操作
#獲取文檔中所有段落的樣式根據樣式進行修改文檔
docu = Document(r'D:/xxx.docx')
for p in docu.paragraphs:
style_name = p.style.name
print(style_name)
#獲取文檔中所有的表格
for tb in docu.tables:
# tb.rows 文檔中所有的行 tb.rows[0].cells 某一行的所有單元格
# 循環(huán)單元格進行編輯樣式操作3.6 其它操作
# word表格單元格背景顏色
def set_cell_background_color(cell, color):
# print(colorStr)
shading_elm_1 = parse_xml(r'<w:shd {} w:fill="{color_value}"/>'.format(nsdecls('w'), color_value=color))
cell._tc.get_or_add_tcPr().append(shading_elm_1)
cells1[i].paragraphs[0].style = "表格體"
# 修改背景顏色為白色
set_cell_background_color(rows.cells[0], "#FFFFFF")
# 查看文檔內所有的樣式
for sts in document.styles:
print(sts)
# 查看word文檔結構
print(document._element.xml)4 總結
最終產生的效果如下圖所示:

在本章中,介紹了怎么使用python-docx創(chuàng)建wor文檔,并舉例說明了創(chuàng)建段落,表格,標題,圖片等要點。
到此這篇關于python辦公之python編輯word的文章就介紹到這了,更多相關python編輯word內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python網絡編程之TCP通信實例和socketserver框架使用例子
這篇文章主要介紹了python網絡編程之TCP通信實例和socketserver框架使用例子,需要的朋友可以參考下2014-04-04
使用python將請求的requests headers參數格式化方法
今天小編就為大家分享一篇使用python將請求的requests headers參數格式化方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01

