首页 > 编程语言 >python-docx高级用法

python-docx高级用法

时间:2023-04-22 14:47:14浏览次数:33  
标签:docx python doc Section style 用法 add paragraph Heading

from docx import Document
from docx.enum.section import WD_SECTION
# 创建一个新文档
doc = Document()
# 添加页眉
header = doc.sections[0].header
header.add_paragraph('Header text')
# 添加页脚
footer = doc.sections[0].footer
footer.add_paragraph('Footer text')
# 设置页眉页脚不同
doc.sections[0].different_first_page_header_footer = True
doc.sections[0].first_page_header.add_paragraph('First page header')
doc.sections[0].first_page_footer.add_paragraph('First page footer')

from docx import Document
from docx.shared import Pt
from docx.oxml.ns import qn
# 创建一个新文档
doc = Document()
# 添加目录
doc.add_paragraph('Table of Contents', style='Heading 1')
doc.add_paragraph('Chapter 1', style='Heading 2')
doc.add_paragraph('Section 1.1', style='Heading 3')
doc.add_paragraph('Section 1.2', style='Heading 3')
doc.add_paragraph('Chapter 2', style='Heading 2')
doc.add_paragraph('Section 2.1', style='Heading 3')
doc.add_paragraph('Section 2.2', style='Heading 3')
doc.add_paragraph('Section 2.3', style='Heading 3')
doc.add_paragraph('Chapter 3', style='Heading 2')
doc.add_paragraph('Section 3.1', style='Heading 3')
doc.add_paragraph('Section 3.2', style='Heading 3')
doc.add_paragraph('Section 3.3', style='Heading 3')
doc.add_paragraph('Section 3.4', style='Heading 3')
doc.add_paragraph('Section 3.5', style='Heading 3')
doc.add_paragraph('Section 3.6', style='Heading 3')
doc.add_paragraph('Section 3.7', style='Heading 3')
# 添加书签
doc.add_paragraph('Bookmark', style='Heading 1')
bookmark = doc.add_paragraph()
bookmark.add_run('This is a bookmarked text.').italic = True
doc.add_bookmark('bookmark', bookmark)
# 更新目录
doc.sections[0].page_width = Pt(700)
doc.sections[0].page_height = Pt(1000)
doc.sections[0].orient = WD_SECTION.LANDSCAPE
doc.sections[0].start_type = WD_SECTION.NEW_PAGE
doc.sections[0].add_page_break()
doc.add_paragraph('Updated Table of Contents', style='Heading 1')
doc.add_paragraph('Chapter 1', style='Heading 2')
doc.add_paragraph('Section 1.1', style='Heading 3')
doc.add_paragraph('Section 1.2', style='Heading 3')
doc.add_paragraph('Chapter 2', style='Heading 2')
doc.add_paragraph('Section 2.1', style='Heading 3')
doc.add_paragraph('Section 2.2', style='Heading 3')
doc.add_paragraph('Section 2.3', style='Heading 3')
doc.add_paragraph('Chapter 3', style='Heading 2')
doc.add_paragraph('Section 3.1', style='Heading 3')
doc.add_paragraph('Section 3.2', style='Heading 3')
doc.add_paragraph('Section 3.3', style='Heading 3')
doc.add_paragraph('Section 3.4', style='Heading 3')
doc.add_paragraph('Section 3.5', style='Heading 3')
doc.add_paragraph('Section 3.6', style='Heading 3')
doc.add_paragraph('Section 3.7', style='Heading 3')
doc.add_paragraph('Bookmark', style='Heading 1')
doc.add_paragraph().add_run('This is a bookmarked text.').italic = True
doc.save('example.docx')

from docx import Document
# 创建一个新文档
doc = Document()
# 添加注释
para = doc.add_paragraph('This is a paragraph with a comment.')
comment = para.add_comment('This is a comment.')
comment.author = 'Author'
# 添加批注
para = doc.add_paragraph('This is a paragraph with a comment.')
doc.add_paragraph('This is another paragraph.')
para.add_run().underline = True
doc.add_comment('This is a comment.', para)
doc.save('example.docx')

from docxtpl import DocxTemplate, InlineImage
# 使用模板生成文档
tpl = DocxTemplate('template.docx')
context = {
    'title': 'Document Title',
    'subtitle': 'Document Subtitle',
    'image': InlineImage(tpl, 'image.jpg', width=500, height=500),
    'table': [
        {'name': 'John', 'age': 30},
        {'name': 'Mary', 'age': 25},
        {'name': 'Tom', 'age': 35}
    ]
}
tpl.render(context)
tpl.save('example.docx')

标签:docx,python,doc,Section,style,用法,add,paragraph,Heading
From: https://www.cnblogs.com/full-stack-linux-new/p/17343039.html

相关文章

  • python数据挖掘与分析实战__电子商务网站用户行为分析及服务推荐
    importosimportpandasaspd#修改工作路径到指定文件夹os.chdir("D:/CourseAssignment/AI/CollectWebDate/")#第一种连接方式fromsqlalchemyimportcreate_engineengine=create_engine('mysql+pymysql://root:123456@localhost:3306/test?charset=utf8')sql......
  • python-pptx用法
    frompptximportPresentation#创建一个新PPT文档prs=Presentation()#添加幻灯片slide=prs.slides.add_slide(prs.slide_layouts[0])#保存PPT文档prs.save('example.pptx')frompptx.utilimportInches#添加文本框slide=prs.slides.add_slide(prs.slide_layouts[1])t......
  • email用法
    importsmtplibfromemail.mime.textimportMIMETextfromemail.mime.multipartimportMIMEMultipartfromemail.mime.imageimportMIMEImage#设置发件人和收件人sender='[email protected]'password='password'receiver='[email protected]'......
  • openpyxl高级用法
    fromopenpyxl.stylesimportFont,Alignment,PatternFill#设置字体样式font=Font(name='Calibri',size=12,bold=True,italic=False,underline='single')ws['A1'].font=font#设置单元格对齐方式alignment=Alignment(horizontal='center......
  • python-docx常用方法
    fromdocximportDocument#创建一个新文档doc=Document()#添加段落para=doc.add_paragraph('HelloWorld!')fromdocx.enum.styleimportWD_STYLE_TYPEfromdocx.enum.textimportWD_ALIGN_PARAGRAPH#添加标题title=doc.add_heading('DocumentTitle',leve......
  • python多进程1:进程池使用时的死锁问题
    一、为什么?你正在使用多进程来在多个进程中运行一些代码,但它却“卡”住了。你查看CPU使用情况,却发现没有任何进展,无法完成任何工作。到底发生了什么?在许多情况下,你只需要加上一行代码就可以解决这个问题(可以跳到最后一句尝试一下),但是首先,让我们深入探讨Python的故障以及P......
  • C语言和C++的switch语句用法
    C语言和C++的switch语句用法是相似的,但在一些细节上有所不同。在C语言中,switch语句的用法如下:switch(expression){  caseconstant1:    //dosomething    break;  caseconstant2:    //dosomething    break;  //...  ......
  • chatGPT,该如何使用python调用你的接口
    以下是chatGPT的回答:好的,这里是一些使用Python调用OpenAIAPI的基本步骤:安装openaiPython包您可以使用pip安装openai包。在终端或命令行中输入以下命令:pipinstallopenai获取API密钥在使用OpenAIAPI之前,您需要获得OpenAIAPI密钥。要获得API密钥,请访问OpenAI网......
  • MySQL 时间类型 date、datetime 和 timestamp 的用法与区别
    时间范围datetime和timestamp区别时间范围不一样,TIMESTAMP要小很多,且最大范围为2038-01-1903:14:07.999999,到期也不远了。datetime与时区无关、timestamp与时区有关。对于timestamp,它把客户端插入的时间从当前时区转化为UTC(世界标准时间)进行存储。查询时,将其又转化为客户......
  • 【python】os模块
    os即operatingsystem,os模块提供的就是各种Python程序与操作系统进行交互的接口。读写文件建议使用内置函数withopen()。导入os模块importos列出路径——os.listdir()os.listdir(path='.')列出目录下的全部路径及文件。函数返回值是一个列表,其中元素为字符串......