我试图使用Python的Python docx模块更改多个段落的格式。在from docx import Document

from docx.shared import Pt

from docx.shared import Inches

from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

from docx.enum.section import WD_ORIENTATION

from content import report_content, provinces, report_date, introduction, intro_content

alignment_dict = {'justify': WD_PARAGRAPH_ALIGNMENT.JUSTIFY,

'center': WD_PARAGRAPH_ALIGNMENT.CENTER,

'centre': WD_PARAGRAPH_ALIGNMENT.CENTER,

'right': WD_PARAGRAPH_ALIGNMENT.RIGHT,

'left': WD_PARAGRAPH_ALIGNMENT.LEFT}

orientation_dict = {'portrait': WD_ORIENTATION.PORTRAIT,

'landscape': WD_ORIENTATION.LANDSCAPE}

document = Document()

def change_orientation(orientation='portrait', set_left_margin=1.0, set_right_margin=1.0):

section = document.sections[-1]

new_width, new_height = section.page_height, section.page_width

section.orientation = orientation_dict[orientation]

section.page_width = new_width

section.page_height = new_height

section.left_margin = Inches(set_left_margin)

section.right_margin = Inches(set_right_margin)

def add_logo(path, align):

document.add_picture(path, width=Inches(4.5), height=Inches(1.5))

last_paragraph = document.paragraphs[-1]

last_paragraph.alignment = alignment_dict[align]

def add_content(content, space_after, font_name='Arial', font_size=11, line_spacing=0, space_before=0,

align='justify', keep_together=True, keep_with_next=False, page_break_before=False,

widow_control=False, set_bold=False, set_italic=False, set_underline=False, set_all_caps=False):

paragraph = document.add_paragraph(content)

style = document.styles['Normal']

font = style.font

font.name = font_name

font.size = Pt(font_size)

font.bold = set_bold

font.italic = set_italic

font.all_caps = set_all_caps

font.underline = set_underline

paragraph_format = paragraph.paragraph_format

paragraph_format.alignment = alignment_dict.get(align.lower())

paragraph_format.space_before = Pt(space_before)

paragraph_format.space_after = Pt(space_after)

paragraph_format.line_spacing = line_spacing

paragraph_format.keep_together = keep_together

paragraph_format.keep_with_next = keep_with_next

paragraph_format.page_break_before = page_break_before

paragraph_format.widow_control = widow_control

def create_numbered_list():

pass

def add_subheading(subheading, level):

document.add_heading(subheading, level)

change_orientation(orientation='landscape', set_left_margin=0.5, set_right_margin=0.5)

add_logo('logo.png', 'center')

add_content(report_content, align='Center', space_before=40, space_after=20, line_spacing=1, font_name='Arial',

set_bold=True, set_all_caps=True)

add_content(provinces, align='Center', space_before=20, space_after=20, line_spacing=1, font_name='Arial',

set_bold=True, set_all_caps=True)

add_content(report_date, align='Center', space_before=20, space_after=20, line_spacing=1, font_name='Arial',

set_bold=True, set_all_caps=True)

document.add_page_break()

add_subheading(introduction, level=1)

add_content(intro_content, space_after=20, space_before=20)

document.save('demo.docx')

问题是,每次我通过add_content方法向新段落块添加格式时,旧块的格式将更改为当前块的格式。在

为什么我不能保留格式,为什么要重置为最新块的格式?在

python docx模块如何删除段落_在pythondocx中更改段落格式相关推荐

  1. python的csv模块自动删除空白_从CSV中删除空白行?

    我有一个很大的csv文件,其中有些行是完全空白的.如何使用Python从csv中删除所有空白行? 经过你的建议,这就是我到目前为止所拥有的import csv # open input csv for ...

  2. python docx 加粗的边框_使用python-docx在表格中指定边框外观

    我正在阅读 python-docx的教程和文档. 但是,我找不到关于如何指定和操作在Microsoft Word文档中创建的表格的边框外观的任何参考. 当我使用以下代码时: from docx imp ...

  3. python docx模块pip安装_python安装docx模块

    python docx 怎么安装 如何安装python docx 直接 pip install docxor python -m pip install docxor sudo python -m p ...

  4. python ssl模块用法详解_一看就懂,Python 日志模块详解及应用!

    日志概述 百度百科的日志概述: Windows网络操作系统都设计有各种各样的日志文件,如应用程序日志,安全日志.系统日志.Scheduler服务日志.FTP日志.WWW日志.DNS服务器日志等等,这些 ...

  5. python pygame模块怎么写游戏_使用 Python 和 Pygame 模块构建一个游戏框架

    这系列的第一篇通过创建一个简单的骰子游戏来探究 Python.现在是来从零制作你自己的游戏的时间. 在我的这系列的第一篇文章 中, 我已经讲解如何使用 Python 创建一个简单的.基于文本的骰子游戏 ...

  6. python zipfile 模块下中文乱码 '╡┌╥╗╒┬_╒╨▒Ω╣½╕µ.docx'

    问题描述: 在使用zipfile读取文件的时候,解压出来的文件名称是乱码,如:'╡┌╥╗╒┬_╒╨▒Ω╣½╕µ.docx' 问题产生原因: 参考资料 There is no official file ...

  7. python docx 合并文档 图片_不再为处理PDF烦恼,python处理操作PDF全攻略

    本篇聊下Python对pdf的各种操作,包含pdf转word,pdf转图片,pdf翻转,加密,加水印等. pdf转换word文档 保留格式 pdf转换为word文档,被大众经常使用的是纯Python库 ...

  8. python docx 合并文档 图片_使用python抓取大量简历文档内数据(word:docx;pdf;图片等)输出表格文件...

    1. 文章背景描述: 近期公司有员工离职了,技术岗位的. 让HR招人,招聘进度也太慢了,实在等不及,就撸起袖子自己上.(之前从来没招聘过) 自己在某招聘网站注册后,花了若干人民币,短时间收到大量求职者 ...

  9. python在列表末尾删除一个_从链接列表的尾部移除(Python)

    我是Python新手,在实现从链接列表中删除最后一个节点的函数时遇到了困难.这是我到目前为止的代码(我在一个名为Coderunner的在线平台上编写代码,该平台已经为我实现了一些背景代码,比如Node ...

最新文章

  1. C语言 —— 预定义符号__FILE____LINE___func__(编写调试函数printf , debug)
  2. jQuery ajax发送POST、JS url跳转、console用法
  3. 牛客第六场 H-Hopping Rabbit
  4. python--采集1(urllib模块)
  5. Spring Batch之批处理实践
  6. php字符串分割汉字,php支持中文字符串分割的函数
  7. hadoop1 hadoop2 fair-schduler 配置和使用
  8. 【硬件解码系列】之ffmpeg硬件加速器
  9. Tricks(十七) —— 数组与字典(map)
  10. Linux下rpm安装jdk17
  11. C#andSql获取当前日期格式
  12. 四阶段法——城市交通需求预测
  13. 字节跳动 Go 语言面试高频题
  14. 移动端一倍图,二倍图尺寸
  15. Hdu-5769 Substring (SA后缀数组)
  16. audio接线图解_5.1家庭影院布线之音响如何接线(图文教程)
  17. 文具行业APS解决方案
  18. 访谈完100个年入百万的自媒体人后,我总结了出了他们的共性
  19. Ubuntu18.04 Ceres Solver
  20. MFC文本编程--退格键的操作

热门文章

  1. 数字地和模拟地处理的连接和处理
  2. PostGIS实现(大数据量,大图斑)图层间相交工具
  3. 最新行政区代码(2010-12-31) SQL语句
  4. Menu G5-不错的跨框架菜单
  5. ESP32设备驱动-HMC5983磁力计驱动
  6. iOS上用GPUImage给视频加滤镜
  7. 最终的一切:创造者作为永久编辑
  8. 痴迷的代价:美国人每年缴纳“苹果税”
  9. 解压ALZ文件的方法
  10. mysql 唯一索引区分大小写