本文同步发表于小弟自架网站(非钓鱼也无广告,纯分享):
微确幸资讯站
最近写了好几篇分享文使用了docx模组来操作word档案,
这个模组功能还蛮强的,趁着这次把docx模组说明文件中一些比较常用的功能翻译成中文。
docx模组英文说明文件网址:
https://python-docx.readthedocs.io/en/latest/index.html
Document constructor (文件建构器)
document = Document()
开启新文件document.save('test.docx')
将word存档为test.docxdocument = Document('test.docx')
开启test.docx档案document.add_heading('文件标题', 0)
新增「文件标题」这些字,样式为word中的「标题」document.add_page_break()
word中的插入分页符号document.add_paragraph()
word中新增段落,可结合tab (\t)、newline (\n)、return (\r)document.add_section()
word中的插入分节符号document.add_picture('photo.jpg', Cm(5))
word中的插入photo.jpg图片,设定图片宽度5公分,须导入from docx.shared import Cmdocument.add_table(3, 2)
word文件中插入3列2栏的表格document.sections()
word文件中的所有节,可以用for section in document.sections:遍历所有的节Section objects(分节物件)
section.different_first_page_header_footer = False
word文件中开启页首或页尾时,首页不同是否打勾。section.header.is_linked_to_previous = True
word文件中开启页首或页尾时,首页是否连结到前一节。section.footer.is_linked_to_previous = True
word文件中开启页首或页尾时,首尾是否连结到前一节。section.page_width = Cm(20)
word文件中设定页面宽度为20公分。须导入from docx.shared import Cmsection.page_height = Cm(30)
word文件中设定页面高度为30公分。须导入from docx.shared import Cmsection.left_margin = Cm(3)
word文件中设定左边界为3公分。须导入from docx.shared import Cmsection.right_margin = Cm(3)
word文件中设定右边界为3公分。须导入from docx.shared import Cmsection.top_margin = Cm(3)
word文件中设定上边界为3公分。须导入from docx.shared import Cmsection.bottom_margin = Cm(3)
word文件中设定下边界为3公分。须导入from docx.shared import CmParagraph objects(段落物件)
run = paragraph.add_run(mytext)
建立run用来处理段落的字体样式等,mytext是加入段落的字串run.bold = True
设定run物件中的mytext为粗体run.font.size = Pt(14)
设定run物件中的mytext字体大小为14,须导入from docx.shared import Pt模组run.font.name = 'Times New Roman'
设定run物件中的mytext英文及数字的字体为Times New Romanrun._element.rPr.rFonts.set(qn('w:eastAsia'), u'标楷体')
设定run物件中的mytext中文字体为标楷体,须导入from docx.oxml.ns import qn模组以下为示範程式码,就不特别示範输出结果了。
要注意的是,document.save()存档后,如果存在一样的档名,会将原档案给覆盖。
import osfrom docx import Documentfrom docx.shared import Cm# 设定工作目录os.chdir('d:/temp/test')# 取得目前工作目录cwd = os.getcwd()# 列印目前工作目录print(cwd)
# 开启新文件且储存document = Document()# 文件存档为test.docxdocument.save('test.docx')
# 开启旧档test.docxdocument = Document('test.docx')# word文件第一层是Document物件,再下一层是是分节物件doc_sections = document.sections# 如果word文件有分1个分节符号,印出来就会有2个section物for section in doc_sections: print(section)
# 开启旧档test.docx,并设定页面与边界document = Document('test.docx')# 假设你的文件中没有分节符号,那定义出第1页(sections[0])后就能对页面做设定doc_section = document.sections[0]# 设定分节页面宽度20公分doc_section.page_width = Cm(20)# 设定分节页面高度50公分doc_section.page_height= Cm(50)# 设定分节页面左边界3公分doc_section.left_margin = Cm(3)# 设定分节页面右边界3公分doc_section.right_margin = Cm(3)# 设定分节页面上边界3公分doc_section.top_margin = Cm(3)# 设定分节页面下边界3公分doc_section.bottom_margin = Cm(3)# 完成设定后存档document.save('test.docx')
# 开启旧档test.docx,并设定页面与边界document = Document('test.docx')# 假设你的文件中有分节符号,你要对所有分节的页面做设定doc_section = document.sections# 遍历所有的分节做设定for section in doc_section: # 设定分节页面宽度20公分 section.page_width = Cm(20) # 设定分节页面高度50公分 section.page_height= Cm(50) # 设定分节页面左边界3公分 section.left_margin = Cm(3) # 设定分节页面右边界3公分 section.right_margin = Cm(3) # 设定分节页面上边界3公分 section.top_margin = Cm(3) # 设定分节页面下边界3公分 section.bottom_margin = Cm(3)# 完成设定后存档document.save('test.docx')
# 开启新文件且储存document = Document()# 文件插入1个分节符号document.add_section()# 文件插入1个分页符号document.add_page_break()# 文件存档为test.docxdocument.save('test.docx')
# 开启新文件且储存document = Document()# 文件中加入「文件标题」的字,样式是标题document.add_heading('文件标题', 0) # 0如果改成1,样式就是标题1# 文件加入「这是示範段落」的字document.add_paragraph('这是示範段落')# 文件加入photo.jpg的图片,设定宽度5公分document.add_picture('photo.jpg', width=Cm(5))# 文件存档为test.docxdocument.save('test.docx')
以上就是这次的分享!!