0
点赞
收藏
分享

微信扫一扫

python 操作word文件案例


基础知识

安装python-docx

pip install python-docx

打开文档

doc = Document('test.docx')

添加段落

paragraph = doc.add_paragraph('段落1')

在段落尾部添加文本

wenben=paragraph.add_run('我是中国人')

获取段落数量

# 段落数量
print(len(doc.paragraphs))

全局字体设置

doc.styles['Normal'].font.name = u'宋体'
doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')

拓展:修改文本中题号
如:1. 2. 3. 类型

all = re.sub(r'[0-9]{1,5}\.', "*", all)

查询括号内的内容

pattern = r'[(](.*?)[)]'
res = re.findall(pattern, i)

删除括号内的内容

i = re.sub(u"\(.*?\)", "( )", i)

文字大小 加粗 颜色

from docx import Document
from docx.oxml.ns import qn
from docx.shared import Pt, RGBColor

doc = Document()

doc = Document()
# 全局字体设置
doc.styles['Normal'].font.name = u'宋体'
doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')

title = doc.add_paragraph()
t1 = title.add_run('公司员工请假条') # 内容
t1.font.bold = True # 加粗
t1.font.size = Pt(20) # 设置字体大小
t1.font.color.rgb = RGBColor(60,200,250) #字体颜色

doc.save('666.docx')

首行缩进:

缩进方式

属性

左边缩进

left_indent

右边缩进

right_indent

首航缩进

first_line_indent

from docx import Document
from docx.shared import Inches

doc = Document()

article = doc.add_paragraph()
a2 = article.add_run(
'因______,特向您请事假____天。请假时间自_____年___月___日至_____年___月___日。这段时间内原计划安排的课程已做好处理,希望领导批准。'
)
# 首行缩进2个字符
article2_format = article.paragraph_format
article2_format.first_line_indent = Inches(0.3)

doc.save('666.docx')

对齐方式:

名称

属性

左对齐

LEFT

居中

CENTER

右对齐

RIGHT

文本两端对齐

JUSTIFY

from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

doc = Document()

title = doc.add_paragraph()
t1 = title.add_run('公司员工请假条') # 内容
title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 居中

doc.save('666.docx')

读取word

word文件:

python 操作word文件案例_python

代码:

from docx import Document

doc = Document('文档.docx')

# 获取段落文字 不带格式
for paragraph in doc.paragraphs:
print(paragraph.text)

# 文字块:获取第二段文字 带格式
paragraph = doc.paragraphs[1] #获取第二段落
runs = paragraph.runs
for run in runs:
print(run.text)

生成word

from docx import Document
from docx.shared import Cm

# 实例化
doc = Document()

# 标题
doc.add_heading("一级标题", level=1)

# 添加一个带格式段落
paragraph1 = doc.add_paragraph()
paragraph1.add_run('加粗').bold = True
paragraph1.add_run('普通')
paragraph1.add_run('斜体').italic = True

# 添加图片
doc.add_picture('tupian.jpg', width=Cm(5), height=Cm(10))

# 添加表格 doc.add_table(rows=行, cols=列)
# cells; 格子
records = [
['学号', '姓名', '成绩'],
['101', '小红', '98'],
['102', '小明', '95'],
['103', '小李', '100']
]
table = doc.add_table(rows=4, cols=3, style='Table Grid')
for row in range(4):
cells = table.rows[row].cells
for col in range(3):
cells[col].text = str(records[row][col])

# 添加分页
doc.add_page_break()

doc.save('测试.docx')

案例:请假条

python 操作word文件案例_word_02

from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx import Document
from docx.shared import Pt, Inches
from docx.oxml.ns import qn

doc = Document()
# 全局字体设置
doc.styles['Normal'].font.name = u'宋体'
doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')

# 1、标题
title = doc.add_paragraph()
title1 = title.add_run('公司员工请假条') # 内容
title1.font.size = Pt(20) # 设置字体大小
title1.bold = True # 加粗
title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # 居中

# 2、正文
article1 = doc.add_paragraph()
a1 = article1.add_run('__________部:')

article2 = doc.add_paragraph()
a2 = article2.add_run(
'因________________________________________,特向您请事假____天。请假时间自_____年___月___日至_____年___月___日。这段时间内原计划安排的课程已做好处理,希望领导批准。'
)
# 首行缩进 负值表示悬挂缩进
article2_format = article2.paragraph_format
article2_format.first_line_indent = Inches(0.3)

article3 = doc.add_paragraph()
a3 = article3.add_run('请假人:') # 内容
article3.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
article3_format = article3.paragraph_format
article3_format.right_indent = Inches(0.9)

nowData = doc.add_paragraph()
n3 = nowData.add_run('年 月 日') # 内容
nowData.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
nowData_format = nowData.paragraph_format
nowData_format.right_indent = Inches(0.3)

# 这一步调整文件格式为居中
doc.save('test.docx')


举报

相关推荐

0 条评论