python辦公之python編輯word
1 前言
在工作中時(shí)常會(huì)有繁重的文案工作,接觸了python 之后,就會(huì)覺(jué)得這個(gè)比較簡(jiǎn)單了,python 操作word 和 excel 是比較常用的操作,相對(duì)比較簡(jiǎn)單,在本文中,我們就以 python 操作 word 為例來(lái)介紹一些簡(jiǎn)單的操作。
2 前提準(zhǔn)備
2.1 python-docx 的安裝
需要操作的前提是下載 docx 相關(guān)的操作類(lèi)庫(kù) 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 文檔的結(jié)構(gòu)說(shuō)明
事先聲明一下,python 操作的word版本必須是 docx
的版本,doc
的文檔暫不支持。另外 docx 文檔也是一種 xml 的數(shù)據(jù)組織格式, 首先了解一下其格式情況,
在word文檔中,其主要結(jié)構(gòu)如下所述:
- 1 每個(gè)document包含多個(gè)paragraph,每個(gè)paragraph有多個(gè)run,每個(gè)run包含有(text文本,font字體,color顏色,字號(hào))
- 2 每個(gè)document包含多個(gè)tables,table中有多個(gè)rows,每個(gè)row包含多個(gè)cells,每個(gè)cell中包含多個(gè)paragraph。對(duì)于寫(xiě)word表格不論是 head 還是paragraph 基本操作都是先添加對(duì)象,然后再添加run就好了
- 3 word表格的結(jié)構(gòu)包含head標(biāo)題、normal 正文、Caption表
3 具體使用
3.1 創(chuàng)建標(biāo)題
# 創(chuàng)建一個(gè)document document = Document() # 創(chuàng)建一個(gè)標(biāo)題 默認(rèn)是一級(jí)標(biāo)題 head = document.add_heading(level=4) run = head.add_run("這是一個(gè)四級(jí)標(biāo)題 this is a title") # font.name 只能設(shè)置西文字體 run.font.name = 'Times New Roman' # 中文字體需要使用這種方式設(shè)置 run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體') # 設(shè)置大小為11磅 run.font.size = Pt(16) # 段落字體顏色 run.font.color.rgb = RGBColor(128, 0, 128) # 是否加粗 run.bold = False # 是否斜體 run.italic = False
3.2 創(chuàng)建段落
# 創(chuàng)建一個(gè)段落 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) # 設(shè)置2.5倍行間距 ph.paragraph_format.line_spacing = 2.5 # 段落縮進(jìn) 段落左縮進(jìn)0.5英寸 left_indent right_indent # p.paragraph_format.left_indent = Inches(0.5) # 首行縮進(jìn) 首行縮進(jìn)0.9cm ph.paragraph_format.first_line_indent = Cm(0.9) # 段落左對(duì)齊 ph.alignment = WD_PARAGRAPH_ALIGNMENT.LEFT run1 = ph.add_run("歷史上第一個(gè)兒子當(dāng)皇帝,老爹還活著的,當(dāng)屬劉太公,也就是劉邦的父親。劉邦建立漢朝,稱帝," "每天還去拜見(jiàn)劉太公,后來(lái)有大臣進(jìn)言講,雖然劉太公貴為皇帝父親,但也為人臣,不應(yīng)該由皇帝前去拜見(jiàn)。") 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)建一個(gè)表格 3行四列 也可以不設(shè)置 table = document.add_table(rows=1, cols=3) # 自動(dòng)調(diào)整表格 table.autofit = True # 設(shè)置表格樣式 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' # 準(zhǔn)備數(shù)據(jù) records = ( (3, '101', 'Spam'), (7, '422', 'Eggs'), (4, '631', 'Spam, spam, eggs, and spam') ) # 添加內(nèi)容 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
3.4 文檔保存
# 保存文檔 指定保存位置 document.save(r"demo_word.docx")
3.5 獲取文檔操作
#獲取文檔中所有段落的樣式根據(jù)樣式進(jìn)行修改文檔 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)單元格進(jì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") # 查看文檔內(nèi)所有的樣式 for sts in document.styles: print(sts) # 查看word文檔結(jié)構(gòu) print(document._element.xml)
4 總結(jié)
最終產(chǎn)生的效果如下圖所示:
在本章中,介紹了怎么使用python-docx創(chuàng)建wor文檔,并舉例說(shuō)明了創(chuàng)建段落,表格,標(biāo)題,圖片等要點(diǎn)。
到此這篇關(guān)于python辦公之python編輯word的文章就介紹到這了,更多相關(guān)python編輯word內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
3個(gè)Python?SQLAlchemy數(shù)據(jù)庫(kù)操作功能詳解
Python?SQLAlchemy?是一個(gè)強(qiáng)大且多功能的?Python?SQL?工具包和對(duì)象關(guān)系映射?(ORM)?系統(tǒng),提供了一整套眾所周知的企業(yè)級(jí)持久性模式,本文為大家整理了它必須了解的3個(gè)數(shù)據(jù)庫(kù)操作功能,希望對(duì)大家有所幫助2023-09-09關(guān)于Python下的Matlab函數(shù)對(duì)應(yīng)關(guān)系(Numpy)
這篇文章主要介紹了關(guān)于Python下的Matlab函數(shù)對(duì)應(yīng)關(guān)系(Numpy),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07python+opencv實(shí)現(xiàn)攝像頭調(diào)用的方法
這篇文章主要為大家詳細(xì)介紹了python+opencv實(shí)現(xiàn)攝像頭調(diào)用的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Python量化因子測(cè)算與繪圖超詳細(xì)流程代碼
這篇文章主要介紹了Python量化因子測(cè)算與繪圖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02PyTorch加載預(yù)訓(xùn)練模型實(shí)例(pretrained)
今天小編就為大家分享一篇PyTorch加載預(yù)訓(xùn)練模型實(shí)例(pretrained),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01