在PythonDocx模块中使用段落函数时减少垂直空间
我正在使用docx.paragration函数向word文档添加类似以下字符串的单行文本=“这只是一些文本”。它工作得很好,除了我想减少文本之间的垂直间距。你知道我该怎么做吗在PythonDocx模块中使用段落函数时减少垂直空间,python,python-2.7,docx,Python,Python 2.7,Docx,我正在使用docx.paragration函数向word文档添加类似以下字符串的单行文本=“这只是一些文本”。它工作得很好,除了我想减少文本之间的垂直间距。你知道我该怎么做吗 谢谢 我创建了一个测试docx,前两段以正常间距隔开,下两段以更大间距隔开: 前两段具有以下结构(document.xml,请参阅) 试验 试验 带有空格的段落如下所示: <w:p w:rsidR="009336B0" w:rsidRDefault="009336B0" w:rsidP="009336B0"&g
谢谢 我创建了一个测试docx,前两段以正常间距隔开,下两段以更大间距隔开: 前两段具有以下结构(document.xml,请参阅)
试验
试验
带有空格的段落如下所示:
<w:p w:rsidR="009336B0" w:rsidRDefault="009336B0" w:rsidP="009336B0">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
</w:pPr>
<w:r>
<w:t>Test2</w:t>
</w:r>
</w:p>
<w:p w:rsidR="009336B0" w:rsidRDefault="009336B0" w:rsidP="009336B0">
<w:pPr>
<w:spacing w:line="480" w:lineRule="auto"/>
</w:pPr>
<w:r>
<w:t>Test2</w:t>
</w:r>
<w:bookmarkStart w:id="0" w:name="_GoBack"/>
<w:bookmarkEnd w:id="0"/>
</w:p>
测试2
测试2
如您所见,有一个
标记包含标记的属性,包括间距
现在,您应该可以通过更改段落函数来更改docx.py
以下是段落功能:
def paragraph(paratext, style='BodyText', breakbefore=False, jc='left'):
"""
Return a new paragraph element containing *paratext*. The paragraph's
default style is 'Body Text', but a new style may be set using the
*style* parameter.
@param string jc: Paragraph alignment, possible values:
left, center, right, both (justified), ...
see http://www.schemacentral.com/sc/ooxml/t-w_ST_Jc.html
for a full list
If *paratext* is a list, add a run for each (text, char_format_str)
2-tuple in the list. char_format_str is a string containing one or more
of the characters 'b', 'i', or 'u', meaning bold, italic, and underline
respectively. For example:
paratext = [
('some bold text', 'b'),
('some normal text', ''),
('some italic underlined text', 'iu')
]
"""
# Make our elements
paragraph = makeelement('p')
if not isinstance(paratext, list):
paratext = [(paratext, '')]
text_tuples = []
for pt in paratext:
text, char_styles_str = (pt if isinstance(pt, (list, tuple))
else (pt, ''))
text_elm = makeelement('t', tagtext=text)
if len(text.strip()) < len(text):
text_elm.set('{http://www.w3.org/XML/1998/namespace}space',
'preserve')
text_tuples.append([text_elm, char_styles_str])
pPr = makeelement('pPr')
pStyle = makeelement('pStyle', attributes={'val': style})
pJc = makeelement('jc', attributes={'val': jc})
pPr.append(pStyle)
pPr.append(pJc)
### Added Content from edi9999:
Here you should add a children to the pPr element
spacing= makeelement('spacing',attributes={'line':480,'lineRule':'auto'})
pPr.append(spacing)
###
# Add the text to the run, and the run to the paragraph
paragraph.append(pPr)
for text_elm, char_styles_str in text_tuples:
run = makeelement('r')
rPr = makeelement('rPr')
# Apply styles
if 'b' in char_styles_str:
b = makeelement('b')
rPr.append(b)
if 'i' in char_styles_str:
i = makeelement('i')
rPr.append(i)
if 'u' in char_styles_str:
u = makeelement('u', attributes={'val': 'single'})
rPr.append(u)
run.append(rPr)
# Insert lastRenderedPageBreak for assistive technologies like
# document narrators to know when a page break occurred.
if breakbefore:
lastRenderedPageBreak = makeelement('lastRenderedPageBreak')
run.append(lastRenderedPageBreak)
run.append(text_elm)
paragraph.append(run)
# Return the combined paragraph
return paragraph
def段落(paratext,style='BodyText',breakbefore=False,jc='left'):
"""
返回一个新的段落元素,其中包含*paratext*。该段落的
默认样式为“正文”,但可以使用
*样式*参数。
@参数字符串jc:段落对齐,可能值:
左、中、右、两侧(对正)。。。
看见http://www.schemacentral.com/sc/ooxml/t-w_ST_Jc.html
要一份完整的清单
如果*paratext*是一个列表,则为每个(文本、字符格式\u str)添加一个运行
列表中的2元组。char\u format\u str是包含一个或多个
字符“b”、“i”或“u”中的一个,表示粗体、斜体和下划线
分别。例如:
副文本=[
('some bold text','b'),
(‘一些普通文本’,“”),
(‘某些斜体下划线文本’、‘iu’)
]
"""
#制造我们的元素
段落=makeelement('p')
如果不存在(副文本,列表):
副文本=[(副文本,)]
text_tuples=[]
对联文中的pt:
text,char_styles_str=(pt if isinstance(pt,(list,tuple))
其他(pt,))
text\u elm=makeelement('t',tagtext=text)
如果len(text.strip())
垂直空间的确切含义是什么?您是指默认设置为1.15的值吗?请您发送两份文件,其中包含您想要的更改(一份没有垂直间距,另一份有),我指的是段落之间的间距。edi9999,感谢您的反馈,但我对如何使用上述内容修改段落功能有点困惑。如果你能提供一些细节。谢谢。类似这样的pStyle=makeelement('pStyle',attributes={'val':style,'line':'0','lineRule':'auto'})不确定这里的间距是否合适?我已经添加了我认为您应该添加的代码(但是没有测试)谢谢,我运行了它,这是我得到的错误:newelement.set(attributenamespace+tagattribute,attributes[tagattribute])文件“lxml.etree.pyx”,第713行,在lxml.etree中。_Element.set(src\lxml\lxml.etree.c:39554)文件“apihelpers.pxi”,第520行,在lxml.etree中。_setAttributeValue(src\lxml\lxml.etree.c:17678)文件“apihelpers.pxi”,第1333行,在lxml.etree.\u utf8(src\lxml\lxml.etree.c:24676)类型错误:参数必须是字节或unicode,得到“int”,请在搜索/测试时多加努力,这是Stackoverflow的思维方式。它只是将数字放入字符串中
def paragraph(paratext, style='BodyText', breakbefore=False, jc='left'):
"""
Return a new paragraph element containing *paratext*. The paragraph's
default style is 'Body Text', but a new style may be set using the
*style* parameter.
@param string jc: Paragraph alignment, possible values:
left, center, right, both (justified), ...
see http://www.schemacentral.com/sc/ooxml/t-w_ST_Jc.html
for a full list
If *paratext* is a list, add a run for each (text, char_format_str)
2-tuple in the list. char_format_str is a string containing one or more
of the characters 'b', 'i', or 'u', meaning bold, italic, and underline
respectively. For example:
paratext = [
('some bold text', 'b'),
('some normal text', ''),
('some italic underlined text', 'iu')
]
"""
# Make our elements
paragraph = makeelement('p')
if not isinstance(paratext, list):
paratext = [(paratext, '')]
text_tuples = []
for pt in paratext:
text, char_styles_str = (pt if isinstance(pt, (list, tuple))
else (pt, ''))
text_elm = makeelement('t', tagtext=text)
if len(text.strip()) < len(text):
text_elm.set('{http://www.w3.org/XML/1998/namespace}space',
'preserve')
text_tuples.append([text_elm, char_styles_str])
pPr = makeelement('pPr')
pStyle = makeelement('pStyle', attributes={'val': style})
pJc = makeelement('jc', attributes={'val': jc})
pPr.append(pStyle)
pPr.append(pJc)
### Added Content from edi9999:
Here you should add a children to the pPr element
spacing= makeelement('spacing',attributes={'line':480,'lineRule':'auto'})
pPr.append(spacing)
###
# Add the text to the run, and the run to the paragraph
paragraph.append(pPr)
for text_elm, char_styles_str in text_tuples:
run = makeelement('r')
rPr = makeelement('rPr')
# Apply styles
if 'b' in char_styles_str:
b = makeelement('b')
rPr.append(b)
if 'i' in char_styles_str:
i = makeelement('i')
rPr.append(i)
if 'u' in char_styles_str:
u = makeelement('u', attributes={'val': 'single'})
rPr.append(u)
run.append(rPr)
# Insert lastRenderedPageBreak for assistive technologies like
# document narrators to know when a page break occurred.
if breakbefore:
lastRenderedPageBreak = makeelement('lastRenderedPageBreak')
run.append(lastRenderedPageBreak)
run.append(text_elm)
paragraph.append(run)
# Return the combined paragraph
return paragraph