本文整理汇总了Python中docx.shared.Pt方法的典型用法代码示例。如果您正苦于以下问题:Python shared.Pt方法的具体用法?Python shared.Pt怎么用?Python shared.Pt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docx.shared
的用法示例。
在下文中一共展示了shared.Pt方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_cell_margins
# 需要导入模块: from docx import shared [as 别名]
# 或者: from docx.shared import Pt [as 别名]
def set_cell_margins(cell: _Cell, margins):
"""
margins{top:, start:, bottom:, end:} sizes in Pt
"""
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
tcMar = OxmlElement('w:tcMar')
for k, m in margins.items():
node = OxmlElement(f'w:{k}')
node.set(qn('w:w'), str(m))
node.set(qn('w:type'), 'dxa')
tcMar.append(node)
tcPr.append(tcMar)
示例2: _decrease_layout_margins
# 需要导入模块: from docx import shared [as 别名]
# 或者: from docx.shared import Pt [as 别名]
def _decrease_layout_margins(self) -> None:
sections = self.document.sections
for section in sections:
section.top_margin = Pt(TOP_MARGIN_PT)
section.bottom_margin = Pt(BOTTOM_MARGIN_PT)
section.left_margin = Pt(LEFT_MARGIN_PT)
section.right_margin = Pt(RIGHT_MARGIN_PT)
示例3: add_header_logos
# 需要导入模块: from docx import shared [as 别名]
# 或者: from docx.shared import Pt [as 别名]
def add_header_logos(self):
# Find the headers
section = self.document.sections[0]
section.header_distance = Pt(0)
header = section.header
table = header.add_table(rows=1, cols=2, width=Inches(24))
table.alignment = WD_TABLE_ALIGNMENT.CENTER
table.autofit = True
left_cell = table.cell(0, 0)
right_cell = table.cell(0, 1)
# Add the left cell to the header
left_image = CellObject(left_cell)
left_cell.paragraphs[-1].alignment = WD_PARAGRAPH_ALIGNMENT.LEFT
left_cell.vertical_alignment = 1
# Add the right cell to the header
right_image = CellObject(right_cell)
right_cell.paragraphs[-1].alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT
right_cell.vertical_alignment = 1
# Add the main logo
left_logo_b64 = self.options.get('demistoLogo', XSOAR_LOGO_BASE64)
s = Section('image', left_logo_b64, {}, {})
image.invoke(left_image, s)
# Add the customer logo
right_logo_b64 = self.options.get('customerLogo', False)
if right_logo_b64:
s = Section('image', right_logo_b64, {}, {
'max_size': {'height': MAX_CUSTOMER_LOGO_HEIGHT_INCH, # max size in inches
'width': MAX_CUSTOMER_LOGO_WIDTH_INCH}})
image.invoke(right_image, s)
示例4: image_for_docx
# 需要导入模块: from docx import shared [as 别名]
# 或者: from docx.shared import Pt [as 别名]
def image_for_docx(fileref, question, tpl, width=None):
if fileref.__class__.__name__ in ('DAFile', 'DAFileList', 'DAFileCollection', 'DALocalFile'):
file_info = dict(fullpath=fileref.path())
else:
file_info = server.file_finder(fileref, convert={'svg': 'png'}, question=question)
if 'fullpath' not in file_info:
return '[FILE NOT FOUND]'
if width is not None:
m = re.search(r'^([0-9\.]+) *([A-Za-z]*)', str(width))
if m:
amount = float(m.group(1))
units = m.group(2).lower()
if units in ['in', 'inches', 'inch']:
the_width = Inches(amount)
elif units in ['pt', 'pts', 'point', 'points']:
the_width = Pt(amount)
elif units in ['mm', 'millimeter', 'millimeters']:
the_width = Mm(amount)
elif units in ['cm', 'centimeter', 'centimeters']:
the_width = Cm(amount)
elif units in ['twp', 'twip', 'twips']:
the_width = Twips(amount)
else:
the_width = Pt(amount)
else:
the_width = Inches(2)
else:
the_width = Inches(2)
return InlineImage(tpl, file_info['fullpath'], the_width)
示例5: transform_for_docx
# 需要导入模块: from docx import shared [as 别名]
# 或者: from docx.shared import Pt [as 别名]
def transform_for_docx(text, question, tpl, width=None):
if type(text) in (int, float, bool, NoneType):
return text
text = str(text)
# m = re.search(r'\[FILE ([^,\]]+), *([0-9\.]) *([A-Za-z]+) *\]', text)
# if m:
# amount = m.group(2)
# units = m.group(3).lower()
# if units in ['in', 'inches', 'inch']:
# the_width = Inches(amount)
# elif units in ['pt', 'pts', 'point', 'points']:
# the_width = Pt(amount)
# elif units in ['mm', 'millimeter', 'millimeters']:
# the_width = Mm(amount)
# elif units in ['cm', 'centimeter', 'centimeters']:
# the_width = Cm(amount)
# elif units in ['twp', 'twip', 'twips']:
# the_width = Twips(amount)
# else:
# the_width = Pt(amount)
# file_info = server.file_finder(m.group(1), convert={'svg': 'png'}, question=question)
# if 'fullpath' not in file_info:
# return '[FILE NOT FOUND]'
# return InlineImage(tpl, file_info['fullpath'], the_width)
# m = re.search(r'\[FILE ([^,\]]+)\]', text)
# if m:
# file_info = server.file_finder(m.group(1), convert={'svg': 'png'}, question=question)
# if 'fullpath' not in file_info:
# return '[FILE NOT FOUND]'
# return InlineImage(tpl, file_info['fullpath'], Inches(2))
#return docassemble.base.filter.docx_template_filter(text, question=question)
return text
示例6: createInvitations
# 需要导入模块: from docx import shared [as 别名]
# 或者: from docx.shared import Pt [as 别名]
def createInvitations(txtFile, docName):
"""Creates invitations based on names in txt file
Args:
txtFile (str): text file to read from
docName (str): doc file to save invitations in
"""
doc = docx.Document()
intro = 'It would be a pleasure to have the company of'
address = 'at 11101 Memory lane on the evening of'
date = 'April 31st'
time = "at 24 O'Clock"
with open(txtFile) as guestList:
for guest in guestList:
name = guest[:-1]
p1 = doc.add_paragraph()
p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
f1 = p1.add_run(intro)
f1.font.bold = True
f1.font.italic = True
f1.font.size = Pt(13)
p2 = doc.add_paragraph()
p2.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
f2 = p2.add_run(name)
f2.font.bold = True
f2.font.size = Pt(15)
p3 = doc.add_paragraph()
p3.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
f3 = p3.add_run(address)
f3.font.bold = True
f3.font.italic = True
f3.font.size = Pt(12)
p4 = doc.add_paragraph()
p4.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
f4 = p4.add_run(date)
f4.font.size = Pt(12)
p5 = doc.add_paragraph()
p5.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
f5 = p5.add_run(time)
f5.font.bold = True
f5.font.italic = True
f5.font.size = Pt(12)
doc.add_page_break()
doc.save(docName)
示例7: _apply_cell_styling
# 需要导入模块: from docx import shared [as 别名]
# 或者: from docx.shared import Pt [as 别名]
def _apply_cell_styling(cell_object: CellObject, section: Section):
style = section.get_style()
# Font size
if PYDOCX_FONT_SIZE in style:
cell_object.run.font.size = Pt(style[PYDOCX_FONT_SIZE])
# Set default font
cell_object.run.font.name = DEFAULT_WORD_FONT
# Font family
if PYDOCX_FONT in style:
cell_object.run.font.name = style[PYDOCX_FONT]
# Other characteristics
if PYDOCX_FONT_BOLD in style:
cell_object.run.font.bold = style[PYDOCX_FONT_BOLD]
if PYDOCX_FONT_STRIKE in style:
cell_object.run.font.strike = style[PYDOCX_FONT_STRIKE]
if PYDOCX_FONT_UNDERLINE in style:
cell_object.run.font.underline = style[PYDOCX_FONT_UNDERLINE]
if PYDOCX_FONT_ITALIC in style:
cell_object.run.font.italic = style[PYDOCX_FONT_ITALIC]
# Font color
if PYDOCX_FONT_COLOR in style:
if style[PYDOCX_FONT_COLOR][0] != '#':
cell_object.run.font.color.rgb = name_to_rgb(
style[PYDOCX_FONT_COLOR])
else:
cell_object.run.font.color.rgb = hex_to_rgb(
style[PYDOCX_FONT_COLOR])
# Background color
if 'backgroundColor' in style:
cell_object.cell = insert_cell_background(
cell_object.cell,
style[PYDOCX_BACKGROUND_COLOR])
# Paragraph styling
if PYDOCX_TEXT_ALIGN in style:
if style[PYDOCX_TEXT_ALIGN] == 'left':
cell_object.paragraph.paragraph_format.alignment = ALIGN_LEFT
elif style[PYDOCX_TEXT_ALIGN] == 'right':
cell_object.paragraph.paragraph_format.alignment = ALIGN_RIGHT
elif style[PYDOCX_TEXT_ALIGN] == 'center':
cell_object.paragraph.paragraph_format.alignment = ALIGN_CENTER
elif style[PYDOCX_TEXT_ALIGN] in [ALIGN_RIGHT, ALIGN_CENTER,
ALIGN_CENTER]:
cell_object.paragraph.paragraph_format.alignment = int(
style[PYDOCX_TEXT_ALIGN])
示例8: create_psmdocx
# 需要导入模块: from docx import shared [as 别名]
# 或者: from docx.shared import Pt [as 别名]
def create_psmdocx(self, l, title, docxname):
'''
:param l list 一组题库
:param title str 页面标题
:param docxname str 题库保存文件名
:return: none
'''
if (title == ''):
page_title = '小学生口算题'
else:
page_title = title
p_docx = Document() # 创建一个docx文档
p_docx.styles['Normal'].font.name = u'Times' # 可换成word里面任意字体
p = p_docx.add_paragraph()
p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER # 段落文字居中设置
run = p.add_run(page_title)
run.font.color.rgb = RGBColor(54, 0, 0) # 颜色设置,这里是用RGB颜色
run.font.size = Pt(self.p_title_size) # 字体大小设置,和word里面的字号相对应
sp = p_docx.add_paragraph()
sp.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER # 段落文字居中设置
srun = sp.add_run(self.p_subtitle)
srun.font.color.rgb = RGBColor(54, 0, 0) # 颜色设置,这里是用RGB颜色
srun.font.size = Pt(self.p_subtitle_size) # 字体大小设置,和word里面的字号相对应
# 判断需要用到的行数
if (len(l) % self.p_column):
rs = len(l) // self.p_column + 2
else:
rs = len(l) // self.p_column +1
# print(rs)
# 将口算题添加到docx表格中
k = 0 # 计数器
table = p_docx.add_table(rows=rs, cols=self.p_column)
for i in range(rs):
if i >0:
row_cells = table.rows[i].cells
for j in range(self.p_column):
if (k > len(l) - 1):
break
else:
row_cells[j].text = l[k]
k = k + 1
table.style.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
table.style.font.color.rgb = RGBColor(54, 0, 0) # 颜色设置,这里是用RGB颜色
table.style.font.size = Pt(self.p_content_siae) # 字体大小设置,和word里面的字号相对应
p_docx.save('{}.docx'.format(docxname)) # 输出docx
示例9: main
# 需要导入模块: from docx import shared [as 别名]
# 或者: from docx.shared import Pt [as 别名]
def main():
repo = git.Repo(search_parent_directories=True)
commit = repo.head.commit
with open('define_template_test_project.py', 'r') as file:
PROJECT_DEFINITION = file.read()
if not os.path.exists(DOC_DIR):
os.makedirs(DOC_DIR)
try:
project = signac.get_project(ext.PROJECT_DIR)
except LookupError:
print('The templates project could not be found. Try running '
'./extract_templates.py before this script.')
raise
sort_keys = list(sorted(project.detect_schema()))
sort_attrs = ['sp.' + '.'.join(key) for key in sort_keys]
def sort_key(job):
values = []
for attr in sort_attrs:
try:
values.append(attrgetter(attr)(job))
except AttributeError:
pass
return [0 if v is None else v for v in values]
environments = project.detect_schema()['environment'][str]
for env in sorted(environments):
env_name = env.split('.')[-1]
document = docx.Document()
# Add code style
style = document.styles.add_style('Code', WD_STYLE_TYPE.PARAGRAPH)
style.font.name = 'Monaco'
style.font.size = Pt(8)
style = document.styles.add_style('CodeChar', WD_STYLE_TYPE.CHARACTER)
style.font.name = 'Monaco'
style.font.size = Pt(8)
document.add_heading(env_name, level=0)
p = document.add_paragraph("Output at commit ")
p.add_run('{}'.format(commit), style='CodeChar')
document.add_heading("FlowProject Definition", level=1)
p = document.add_paragraph(PROJECT_DEFINITION, style='Code')
document.add_page_break()
document.add_heading("Operations without bundling", level=1)
query = {'environment': env, 'parameters.bundle': {'$exists': False}}
for job in sorted(project.find_jobs(query), key=sort_key):
process_job(document, job)
document.add_heading("Operations with bundling", level=1)
query = {'environment': env, 'parameters.bundle': {'$exists': True}}
for job in sorted(project.find_jobs(query), key=sort_key):
process_job(document, job)
fn = os.path.join(DOC_DIR, "{env}.docx".format(env=env_name))
document.save(fn)
print("Generated document '{}'.".format(fn))