from docx import Document # 導(dǎo)入docx包
from docx.shared import Cm, Inches, Pt, RGBColor # 導(dǎo)入單位換算函數(shù)
document = Document('李虛中命書(shū).docx') # 新建word文檔
section = document.sections[0] # 獲取section對(duì)象
#修改頁(yè)面大小
# print('默認(rèn)頁(yè)面的寬度和高度:', section.page_width.cm,section.page_height.cm) # 打印默認(rèn)頁(yè)面寬度和高度
# section.page_width = Cm(40)
# section.page_height = Cm(40)
# print('修改后頁(yè)面的寬度和高度:', section.page_width.cm, section.page_height.cm) # 打印修改后的頁(yè)面寬度和高度
#修改頁(yè)邊距
print('上頁(yè)邊距:下頁(yè)邊距:左頁(yè)邊距:右頁(yè)邊距:' ,section.top_margin , section.bottom_margin, section.left_margin,section.right_margin) #:上頁(yè)邊距#:下頁(yè)邊距#:左頁(yè)邊距#:右頁(yè)邊距
section.top_margin = Cm(1.27)
section.bottom_margin = Cm(1.27)
section.left_margin = Cm(1.27)
section.right_margin = Cm(1.27)
print('上頁(yè)邊距:下頁(yè)邊距:左頁(yè)邊距:右頁(yè)邊距:' ,section.top_margin , section.bottom_margin, section.left_margin,section.right_margin) #:上頁(yè)邊距#:下頁(yè)邊距#:左頁(yè)邊距#:右頁(yè)邊距
#修改紙張方向
# from docx.enum.section import WD_ORIENTATION
# WD_ORIENTATION.LANDSCAPE:紙張方向?yàn)闄M向。
# WD_ORIENTATION.PORTRAIT:紙張方向?yàn)榭v向。
# section.orientation = WD_ORIENTATION.LANDSCAPE
#設(shè)置分欄
# from docx.oxml.ns import qn
# section._sectPr.xpath('./w:cols')[0].set(qn('w:num'), '2') #設(shè)置為2欄
#設(shè)置頁(yè)眉頁(yè)腳
# header = section.header # 獲取第一個(gè)節(jié)的頁(yè)眉
# print('頁(yè)眉中默認(rèn)段落數(shù):', len(header.paragraphs))
# paragraph = header.paragraphs[0] # 獲取頁(yè)眉的第一個(gè)段落
# paragraph.add_run('這是第一節(jié)的頁(yè)眉2') # 添加頁(yè)面內(nèi)容
# footer = section.footer # 獲取第一個(gè)節(jié)的頁(yè)腳
# paragraph = footer.paragraphs[0] # 獲取頁(yè)腳的第一個(gè)段落
# paragraph.add_run('這是第一節(jié)的頁(yè)腳2') # 添加頁(yè)腳內(nèi)容
# 段落樣式修改
# 新增加一段段落
# paragraph = document.add_paragraph('這是一個(gè)新的段落\n 這又是一個(gè)段落哦')
# 設(shè)置段落間距
# space_before,space_after 上下間距
# line_spacing,line_spacing_rule 行距
# paragraph_format=paragraph.paragraph_format
# paragraph_format.space_before=Pt(0) #上行間距
# paragraph_format.space_after=Pt(0) #下行間距
# paragraph_format.line_spacing=1.15 #行距
# from docx.enum.text import WD_LINE_SPACING
# ONE_POINT_FIVE,1.5倍行距
# AT_LEAST,最小行距
# DOUBLE,雙倍行距
# EXACTLY,固定值
# MULTIPLE,多倍行距
# SINGL,單倍行距
# paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE
#整體樣式調(diào)整
from docx.oxml.ns import qn
document.styles['Normal'].font.name = u'宋體'
document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋體')
document.styles['Normal'].font.size = Pt(10.5)
document.styles['Normal'].font.color.rgb = RGBColor(0,80,0)
#標(biāo)題樣式調(diào)整
# 標(biāo)題等級(jí)0-9這些數(shù)字,一級(jí)標(biāo)題二級(jí)標(biāo)題這樣
Head = document.add_heading("",level=1)# 這里不填標(biāo)題內(nèi)容
run = Head.add_run("真正要填寫(xiě)的標(biāo)題")
run.font.name=u'Cambria'
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'Cambria')
run.font.color.rgb = RGBColor(0,0,0)
import docx
para_heading.alignment = docx.enum.text.WD_ALIGN_PARAGRAPH.CENTER #設(shè)置為居中
#段落字體樣式
from docx.enum.style import WD_STYLE_TYPE
from docx.oxml.ns import qn
page = document.add_paragraph('這是一個(gè)段落')
run = page.add_run('這是一個(gè)段落')
run.font.name='微軟雅黑' #字體名稱
run.font.size=Pt(50) #字體大小
run.font.color.rgb=RGBColor(0,0,0) #字體顏色
from docx.enum.dml import MSO_THEME_COLOR_INDEX, MSO_COLOR_TYPE
# 內(nèi)置顏色
# BACKGROUND_1、BACKGROUND_2、TEXT_1、TEXT_2、ACCENT_1、ACCENT_2、ACCENT_3、ACCENT_4、ACCENT_5和ACCENT_6
run.font.color.theme_color =MSO_THEME_COLOR_INDEX.ACCENT_1 # 設(shè)置字體主題顏色
run.font.highlight_color = MSO_THEME_COLOR_INDEX.DARK_1 # 設(shè)置高亮背景顏色
# 加粗
# run.bold = True
# 斜體
# run.font.italic = True
# 設(shè)置下劃線
# run.font.underline = True
# 設(shè)置輪廓線
# run.font.outline = True
# 設(shè)置陰影
# run.font.shadow = True
# 刪除線
# run.font.strike = True
# 雙刪除線
# run.font.double_strike = True
# 設(shè)置下標(biāo)
# run.font.subscript = True
# 設(shè)置上標(biāo)
# run.font.superscript = True
#結(jié)束保存
document.save('test.docx') # 保存word文檔
# 字號(hào)‘八號(hào)’對(duì)應(yīng)磅值5
# 字號(hào)‘七號(hào)’對(duì)應(yīng)磅值5.5
# 字號(hào)‘小六’對(duì)應(yīng)磅值6.5
# 字號(hào)‘六號(hào)’對(duì)應(yīng)磅值7.5
# 字號(hào)‘小五’對(duì)應(yīng)磅值9
# 字號(hào)‘五號(hào)’對(duì)應(yīng)磅值10.5
# 字號(hào)‘小四’對(duì)應(yīng)磅值12
# 字號(hào)‘四號(hào)’對(duì)應(yīng)磅值14
# 字號(hào)‘小三’對(duì)應(yīng)磅值15
# 字號(hào)‘三號(hào)’對(duì)應(yīng)磅值16
# 字號(hào)‘小二’對(duì)應(yīng)磅值18
# 字號(hào)‘二號(hào)’對(duì)應(yīng)磅值22
# 字號(hào)‘小一’對(duì)應(yīng)磅值24
# 字號(hào)‘一號(hào)’對(duì)應(yīng)磅值26
# 字號(hào)‘小初’對(duì)應(yīng)磅值36
# 字號(hào)‘初號(hào)’對(duì)應(yīng)磅值42
# 讀取docx
document = Document('file')
for i in document.paragraphs:
print(i.text)