python docx模块如何删除段落_在pythondocx中更改段落格式
我试图使用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中更改段落格式相关推荐
- python的csv模块自动删除空白_从CSV中删除空白行?
我有一个很大的csv文件,其中有些行是完全空白的.如何使用Python从csv中删除所有空白行? 经过你的建议,这就是我到目前为止所拥有的import csv # open input csv for ...
- python docx 加粗的边框_使用python-docx在表格中指定边框外观
我正在阅读 python-docx的教程和文档. 但是,我找不到关于如何指定和操作在Microsoft Word文档中创建的表格的边框外观的任何参考. 当我使用以下代码时: from docx imp ...
- python docx模块pip安装_python安装docx模块
python docx 怎么安装 如何安装python docx 直接 pip install docxor python -m pip install docxor sudo python -m p ...
- python ssl模块用法详解_一看就懂,Python 日志模块详解及应用!
日志概述 百度百科的日志概述: Windows网络操作系统都设计有各种各样的日志文件,如应用程序日志,安全日志.系统日志.Scheduler服务日志.FTP日志.WWW日志.DNS服务器日志等等,这些 ...
- python pygame模块怎么写游戏_使用 Python 和 Pygame 模块构建一个游戏框架
这系列的第一篇通过创建一个简单的骰子游戏来探究 Python.现在是来从零制作你自己的游戏的时间. 在我的这系列的第一篇文章 中, 我已经讲解如何使用 Python 创建一个简单的.基于文本的骰子游戏 ...
- python zipfile 模块下中文乱码 '╡┌╥╗╒┬_╒╨▒Ω╣½╕µ.docx'
问题描述: 在使用zipfile读取文件的时候,解压出来的文件名称是乱码,如:'╡┌╥╗╒┬_╒╨▒Ω╣½╕µ.docx' 问题产生原因: 参考资料 There is no official file ...
- python docx 合并文档 图片_不再为处理PDF烦恼,python处理操作PDF全攻略
本篇聊下Python对pdf的各种操作,包含pdf转word,pdf转图片,pdf翻转,加密,加水印等. pdf转换word文档 保留格式 pdf转换为word文档,被大众经常使用的是纯Python库 ...
- python docx 合并文档 图片_使用python抓取大量简历文档内数据(word:docx;pdf;图片等)输出表格文件...
1. 文章背景描述: 近期公司有员工离职了,技术岗位的. 让HR招人,招聘进度也太慢了,实在等不及,就撸起袖子自己上.(之前从来没招聘过) 自己在某招聘网站注册后,花了若干人民币,短时间收到大量求职者 ...
- python在列表末尾删除一个_从链接列表的尾部移除(Python)
我是Python新手,在实现从链接列表中删除最后一个节点的函数时遇到了困难.这是我到目前为止的代码(我在一个名为Coderunner的在线平台上编写代码,该平台已经为我实现了一些背景代码,比如Node ...
最新文章
- C语言 —— 预定义符号__FILE____LINE___func__(编写调试函数printf , debug)
- jQuery ajax发送POST、JS url跳转、console用法
- 牛客第六场 H-Hopping Rabbit
- python--采集1(urllib模块)
- Spring Batch之批处理实践
- php字符串分割汉字,php支持中文字符串分割的函数
- hadoop1 hadoop2 fair-schduler 配置和使用
- 【硬件解码系列】之ffmpeg硬件加速器
- Tricks(十七) —— 数组与字典(map)
- Linux下rpm安装jdk17
- C#andSql获取当前日期格式
- 四阶段法——城市交通需求预测
- 字节跳动 Go 语言面试高频题
- 移动端一倍图,二倍图尺寸
- Hdu-5769 Substring (SA后缀数组)
- audio接线图解_5.1家庭影院布线之音响如何接线(图文教程)
- 文具行业APS解决方案
- 访谈完100个年入百万的自媒体人后,我总结了出了他们的共性
- Ubuntu18.04 Ceres Solver
- MFC文本编程--退格键的操作