1、說明
Python 中可以讀取 word 文件的庫有 python-docx 和 pywin32。
下表比較了各自的優缺點。
優點 |
缺點 |
|
---|---|---|
python-docx |
跨平台 |
只能處理 .docx 格式,不能處理.doc格式 |
pywin32 |
僅限 windows 平台 |
.doc 和 .docx 都能處理 |
2、下載
python -m pip install python-docx
3、添加1-9級標題
from datetime import datetime from docx import Document # 創建新的docx文件 document = Document() document.add_heading('1級標題', 1) # 添加1級標題 document.add_heading('2級標題', 2) # 添加2級標題 document.add_heading('3級標題', 3) # 添加3級標題 document.add_heading('4級標題', 4) # 添加4級標題 document.add_heading('5級標題', 5) # 添加5級標題 document.add_heading('6級標題', 6) # 添加6級標題 document.add_heading('7級標題', 7) # 添加7級標題 document.add_heading('8級標題', 8) # 添加8級標題 document.add_heading('9級標題', 9) # 添加9級標題 document.save('{}.docx'.format(datetime.now().strftime('%Y%m%d%H%M%S')))
運行截圖
4、添加段落
from datetime import datetime from docx import Document # 創建新的docx文件 document = Document() paragraph = """這是一個段落 """ paragraph2 = """這是一個新的段落""" paragraph3 = """這是一個新的段落。 """ document.add_paragraph(paragraph) document.add_paragraph(paragraph2) document.add_paragraph(paragraph3) document.save('{}.docx'.format(datetime.now().strftime('%Y%m%d%H%M%S')))
運行截圖
5、設置字體大小和樣式
from datetime import datetime from docx import Document # 創建新的docx文件 from docx.shared import Pt document = Document() document.add_paragraph("這是一個段落") # 添加段落 paragraph = document.add_paragraph("這是一個段落,") # 添加段落 run = paragraph.add_run('設置了字體的段落') # 沒理解,可以看官方介紹,如下: """ Append a run to this paragraph containing *text* and having character style identified by style ID *style*. *text* can contain tab (``\\t``) characters, which are converted to the appropriate XML form for a tab. *text* can also include newline (``\\n``) or carriage return (``\\r``) characters, each of which is converted to a line break. 大概意思就是追加一個段落,包含text,且設置了格式,我感覺是這樣 """ run.font.name = u'宋體' # 設置字體 run.font.size = Pt(20) # 設置字號 run1 = paragraph.add_run('\t粗體') run1.bold = True run2 = paragraph.add_run('\t斜體') run2.italic = True document.save('{}.docx'.format(datetime.now().strftime('%Y%m%d%H%M%S')))
運行截圖
6、有序(無序)列表和引用
from datetime import datetime from docx import Document # 創建新的docx文件 document = Document() # 增加引用 document.add_paragraph('123', style='Intense Quote') # 增加有序列表 document.add_paragraph(u'有序列表元素1', style='List Number') document.add_paragraph(u'有序列別元素2', style='List Number') # 增加無序列表 document.add_paragraph(u'無序列表元素1', style='List Bullet') document.add_paragraph(u'無序列表元素2', style='List Bullet') document.save('{}.docx'.format(datetime.now().strftime('%Y%m%d%H%M%S')))
運行截圖
7、表格和分頁
from datetime import datetime from docx import Document # 創建新的docx文件 document = Document() # 增加圖片(此處使用相對位置) # document.add_picture('jdb.jpg', width=Inches(1.25)) # 增加表格 table = document.add_table(rows=3, cols=3) # 3行3列 hdr_cells1 = table.rows[0].cells # 第一行 hdr_cells1[0].text = "第一行,第一列" hdr_cells1[1].text = "第一行,第二列" hdr_cells1[2].text = "第一行,第三列" hdr_cells2 = table.rows[1].cells # 第二行 hdr_cells2[0].text = "第二行,第一列" hdr_cells2[1].text = "第二行,第二列" hdr_cells2[2].text = "第二行,第三列" hdr_cells3 = table.rows[2].cells # 第三行 hdr_cells3[0].text = "第三行,第一列" hdr_cells3[1].text = "第三行,第二列" hdr_cells3[2].text = "第三行,第三列" # 增加分頁 document.add_page_break() document.save('{}.docx'.format(datetime.now().strftime('%Y%m%d%H%M%S')))
運行截圖
參考鏈接 : https://cloud.tencent.com/developer/article/1578773 和 https://www.jianshu.com/p/1f60cdd9655a