字体和 table python-docx 模块的问题
Issues with font and table python-docx module
Python = 3.8.3
Python-Docx = 0.8.10
我目前正在尝试通过编写我的第一个项目来为我的妻子做一个 present/surprise,这将通过自动化她的写作评估来减轻她的工作负担(目前学习 python 从 6 个月开始)。
长话短说,她正在根据不同能力的标记对 word docx 文档进行评估。我的想法是使输入模板按能力排序(A、B、C、D ......),每个模板将包含根据标记(低分、平均分、高分)的文本块。
我做了一些研究并找到了 python-Docx 模块,我可以使用它自动将输入文档模板的副本生成最终输出文档。
在做我的项目时,我目前面临两个大问题,首先我无法保留她使用的原始字体(标题为 Helvetica,文本为 Garamond),第二个问题是,她有一些她文档中的表格我也无法复制。
到目前为止,我设法根据标记复制了正确的文本块,并保留了粗体、斜体和下划线,但没有保留字体和表格。
我一直在在线阅读 python-docx 手册,但我真的不知道在哪里可以修复这些 2.
这是我的代码示例:
import os
import sys
from docx import Document
from docxtpl import DocxTemplate
# Imports
inputM01acgti = Document(
'/home/rvera/ubuntuShare/python_Projects/docx_tpl/M-01-ACG-TI-02.docx')
# Output document
output_doc = Document()
# Function to get proper format of original templates
def get_para_data(output_doc_name, paragraph):
output_para = output_doc_name.add_paragraph()
for run in paragraph.runs:
output_run = output_para.add_run(run.text)
# Run's bold data
output_run.bold = run.bold
# Run's italic data
output_run.italic = run.italic
# Run's underline data
output_run.underline = run.underline
# Run's color data
output_run.font.color.rgb = run.font.color.rgb
# Run's font data
output_run.style.name = run.style.name
# Paragraph's alignment data
output_para.paragraph_format.alignment = paragraph.paragraph_format.alignment
# Function for Male Assement
def maleAssement():
print('Aptitude Cognitive Globale')
acgMark = int(input('Please enter a number: '))
if 0 <= acgMark < 2:
paragraphs_acg = inputM01acgti.paragraphs
elif 2 <= acgMark < 16:
paragraphs_acg = inputM01acgmi.paragraphs
elif 16 <= acgMark < 86:
paragraphs_acg = inputM01acgm.paragraphs
elif 16 <= acgMark < 98:
paragraphs_acg = inputM01acgms.paragraphs
elif 98 <= acgMark <= 100:
paragraphs_acg = inputM01acgts.paragraphs
else:
print('Your number is not between 0 and 100, try again..')
for para in paragraphs_acg:
get_para_data(output_doc, para)
如您所见,我进行了不同的运行以保持输入文档的样式,但我缺少字体和表格。
有人可以帮我吗?
非常感谢!
Xzi.
这是保持字体和字体大小的正确方法:
# Function to get proper format of original templates
def get_para_data(output_doc_name, 段落):
output_para = output_doc_name.add_paragraph()
for run in paragraph.runs:
output_run = output_para.add_run(run.text)
# Run's bold data
output_run.bold = run.bold
# Run's italic data
output_run.italic = run.italic
# Run's underline data
output_run.underline = run.underline
# Run's color data
output_run.font.color.rgb = run.font.color.rgb
# Run's font
output_run.font.name = run.font.name
# Run's font size
output_run.font.size = run.font.size
# Run's font data
output_run.style.name = run.style.name
# Paragraph's alignment data
#output_para.style = paragraph.style
#output_para.alignment = paragraph.alignment
output_para.paragraph_format.alignment = paragraph.paragraph_format.alignment
#output_para.paragraph_format.widow_control = paragraph.paragraph_format.widow_control
Python = 3.8.3 Python-Docx = 0.8.10
我目前正在尝试通过编写我的第一个项目来为我的妻子做一个 present/surprise,这将通过自动化她的写作评估来减轻她的工作负担(目前学习 python 从 6 个月开始)。
长话短说,她正在根据不同能力的标记对 word docx 文档进行评估。我的想法是使输入模板按能力排序(A、B、C、D ......),每个模板将包含根据标记(低分、平均分、高分)的文本块。
我做了一些研究并找到了 python-Docx 模块,我可以使用它自动将输入文档模板的副本生成最终输出文档。
在做我的项目时,我目前面临两个大问题,首先我无法保留她使用的原始字体(标题为 Helvetica,文本为 Garamond),第二个问题是,她有一些她文档中的表格我也无法复制。
到目前为止,我设法根据标记复制了正确的文本块,并保留了粗体、斜体和下划线,但没有保留字体和表格。
我一直在在线阅读 python-docx 手册,但我真的不知道在哪里可以修复这些 2.
这是我的代码示例:
import os
import sys
from docx import Document
from docxtpl import DocxTemplate
# Imports
inputM01acgti = Document(
'/home/rvera/ubuntuShare/python_Projects/docx_tpl/M-01-ACG-TI-02.docx')
# Output document
output_doc = Document()
# Function to get proper format of original templates
def get_para_data(output_doc_name, paragraph):
output_para = output_doc_name.add_paragraph()
for run in paragraph.runs:
output_run = output_para.add_run(run.text)
# Run's bold data
output_run.bold = run.bold
# Run's italic data
output_run.italic = run.italic
# Run's underline data
output_run.underline = run.underline
# Run's color data
output_run.font.color.rgb = run.font.color.rgb
# Run's font data
output_run.style.name = run.style.name
# Paragraph's alignment data
output_para.paragraph_format.alignment = paragraph.paragraph_format.alignment
# Function for Male Assement
def maleAssement():
print('Aptitude Cognitive Globale')
acgMark = int(input('Please enter a number: '))
if 0 <= acgMark < 2:
paragraphs_acg = inputM01acgti.paragraphs
elif 2 <= acgMark < 16:
paragraphs_acg = inputM01acgmi.paragraphs
elif 16 <= acgMark < 86:
paragraphs_acg = inputM01acgm.paragraphs
elif 16 <= acgMark < 98:
paragraphs_acg = inputM01acgms.paragraphs
elif 98 <= acgMark <= 100:
paragraphs_acg = inputM01acgts.paragraphs
else:
print('Your number is not between 0 and 100, try again..')
for para in paragraphs_acg:
get_para_data(output_doc, para)
如您所见,我进行了不同的运行以保持输入文档的样式,但我缺少字体和表格。
有人可以帮我吗?
非常感谢!
Xzi.
这是保持字体和字体大小的正确方法:
# Function to get proper format of original templates
def get_para_data(output_doc_name, 段落):
output_para = output_doc_name.add_paragraph()
for run in paragraph.runs:
output_run = output_para.add_run(run.text)
# Run's bold data
output_run.bold = run.bold
# Run's italic data
output_run.italic = run.italic
# Run's underline data
output_run.underline = run.underline
# Run's color data
output_run.font.color.rgb = run.font.color.rgb
# Run's font
output_run.font.name = run.font.name
# Run's font size
output_run.font.size = run.font.size
# Run's font data
output_run.style.name = run.style.name
# Paragraph's alignment data
#output_para.style = paragraph.style
#output_para.alignment = paragraph.alignment
output_para.paragraph_format.alignment = paragraph.paragraph_format.alignment
#output_para.paragraph_format.widow_control = paragraph.paragraph_format.widow_control