**安裝依賴:**
```
python辦公自動化書籍?pip install python-docx
```
**簡單的寫入一點內容:**
python如何辦公,```
import docx
file=docx.Document() #創建內存中的word文檔對象
python處理word。file.add_paragraph("窗前明月光") #寫入若干段落
file.add_paragraph("疑是地上霜")
file.add_paragraph("舉頭望明月")
python自動。file.add_paragraph("低頭思故鄉")
file.save("E:\靜夜思.docx") #保存才能看到結果
```
Python word?**添加的時候可以寫入樣式,表格等**:
```
#插入有序列表,段落的前面會有序號123
爬蟲自動化辦公、doc2.add_paragraph('把冰箱門打開',style='List Number')
doc2.add_paragraph('把大象裝進去',style='List Number')
doc2.add_paragraph('把冰箱門關上',style='List Number')
python編輯word文檔。#插入無序列表,段落的前面沒有序號
doc2.add_paragraph('AA',style='List Bullet')
doc2.add_paragraph('BB',style='List Bullet')
doc2.add_paragraph('CC',style='List Bullet')
#插入一個6行6列的表格
table=doc2.add_table(rows=6,cols=6,style='Table Grid')
for i in range(0,6):
for j in range(0,6):
table.cell(i,j).text="第{i}行{j}列".format(i=i+1,j=j+1)
#插入照片
doc2.add_picture('FLAMING MOUNTAIN.JPG',width=docx.shared.Inches(5))
```
**添加段落的樣式有這么多可以選擇的,具體的效果大家可以試試:**
```
Normal
Body Text
Body Text 2
Body Text 3
Caption
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Heading 7
Heading 8
Heading 9
Intense Quote
List
List 2
List 3
List Bullet
List Bullet 2
List Bullet 3
List Continue
List Continue 2
List Continue 3
List Number
List Number 2
List Number 3
List Paragraph
Macro Text
No Spacing
Quote
Subtitle
TOCHeading
Title
```
段落樣式,表格樣式等官方文檔:
https://python-docx.readthedocs.io/en/latest/user/styles-understanding.html
**設置頁眉:**
```
file=docx.Document() #創建內存中的word文檔對象
head=file.sections[0].header
head.paragraphs[0].text="Left Text\tCenter Text\tRight Text"
head.paragraphs[0].style =file.styles["Header"]
head.is_linked_to_previous=False
```
設置頁眉的官方文檔:
https://python-docx.readthedocs.io/en/latest/user/hdrftr.html
**設置字體樣式(字體與大小)**
```
from docx.shared import Pt
file=docx.Document() #創建內存中的word文檔對象
#設置字體樣式(字體大小與)
mystyle = file.styles["Header"]
font = mystyle.font
font.size = Pt(10.5)
font.name = '宋體'
head.paragraphs[0].style = mystyle
```
其中字號與數字的對應如下:
>初號=42磅 小初=36磅 一號=26磅 小一=24磅?二號=22磅 小二=18磅 三號=16磅 小三=15磅?四號=14磅 小四=12磅 五號=10.5磅 小五=9磅?六號=7.5磅 小六=6.5磅 七號=5.5磅 八號=5磅?
tn> 注意設置中文字體的時候直接是不行的
需要使用如下的寫法,兩個一起用
```
#這里需要引入一個qn
from docx.oxml.ns import qn
mystyle = file.styles["Normal"]
font = mystyle.font
font.size = Pt(10.5)
font.name = u'宋體'
font._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')
head.paragraphs[0].style = mystyle
```
當然如果想全局設置字體的話可以這樣做
```
from docx.oxml.ns import qn
document.styles['Normal'].font.name = u'宋體'
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')
```
>docx內置的樣式都可以通過document.styles取到。
正文是Normal, 標題樣式根據標題聲明的基本,分別從Heading 1 到Heading 9, 另外還有table、list等各種word對應的樣式。可以通過遍歷document.styles查看。
**標題與字體**
```
# 導入庫
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from docx.oxml.ns import qn
from docx.shared import RGBColor
# 新建空白文檔
doc1 = Document()
# 新增文檔標題
doc1.add_heading('如何使用 Python 創建 Word',0)
# 創建段落描述
doc1.add_paragraph('我們平時使用 Word 用來做文章的處理,可能沒想過它可以用 Python 生成,下面我們就介紹具體如何操作……')
# 創建一級標題
doc1.add_heading('安裝 python-docx 庫',1)
# 創建段落描述
doc1.add_paragraph('現在開始我們來介紹如何安裝 python-docx 庫,具體需要以下兩步操作:')
# 創建二級標題
doc1.add_heading('第一步:安裝 Python',2)
# 創建段落,添加文檔內容
paragraph = doc1.add_paragraph('這是第一步的安裝描述!')
# 段落中增加文字,并設置字體字號
run = paragraph.add_run('(注意:這里設置了字號為20)')
run.font.size = Pt(20)
# 設置英文字體
run = doc1.add_paragraph('這里設置英文字體:').add_run('This Font is Times New Roman ')
run.font.name = 'Times New Roman'
# 設置中文字體
run = doc1.add_paragraph('這里設置中文字體:').add_run('當前字體為黑體')
run.font.name='黑體'
r = run._element
r.rPr.rFonts.set(qn('w:eastAsia'), '黑體')
# 設置斜體
run = doc1.add_paragraph('這段設置:').add_run('文字的是斜體 ')
run.italic = True
# 設置粗體
run = doc1.add_paragraph('這段再設置:').add_run('這里設置粗體').bold = True
# 設置字體帶下劃線
run = doc1.add_paragraph('這段為下劃線:').add_run('這里設置帶下劃線').underline = True
# 設置字體顏色
run = doc1.add_paragraph('這段字體為紅色:').add_run('這里設置字體為紅色')
run.font.color.rgb = RGBColor(0xFF, 0x00, 0x00)
# 增加引用
doc1.add_paragraph('axj', style='Intense Quote')
# 保存文件
doc1.save('myword.docx')
```
官方文檔:
https://python-docx.readthedocs.io/en/latest/user/styles-using.html
**設置段落行高與對齊方式等**
對齊方式修改
```
from docx.enum.text import WD_ALIGN_PARAGRAPH
paragraph.alignment=WD_ALIGN_PARAGRAPH.CENTER # 居中對齊
```
> paragraph.alignment=對齊方式,可選的對齊方式:LEFT、CENTER、RIGHT、JUSTIFY、DISTRIBUTE、JUSTIFY_MED、JUSTIFY_HI、JUSTIFY_LOW、THAI_JUSTIFY,具體的效果自己嘗試
行間距設置
```
paragraph.paragraph_format.line_spacing=2.0 # 修改行間距
```
>使用paragraph.paragraph_format.line_spacing=2.0的方法,修改行間距,使用浮點數,2.0就表示兩倍行間距!
修改段落間距
```
paragraph.paragraph_format.space_before=Pt(12)
paragraph.paragraph_format.space_after=Pt(10)
```
> 修改段前和段后間距,使用以上代碼。Pt(12)表示12磅
注意設置了行間距后要把:在相同樣式的段落間不添加空格勾勾去掉才能起作用,不然會有問題的
![](https://img.tnblog.net/arcimg/aojiancc2/bd9dcf32b2df406c9e186c6a1e8c7675.png)
tn> 設置樣式這些還沒有玩得很轉,不然配合python強大的爬蟲能力真的可以減少好多工作量
**其他的可以參考一下官方文檔**:
https://python-docx.readthedocs.io/en/latest/
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态