【问题标题】:python-pptx change font size in a Tablepython-pptx 更改表格中的字体大小
【发布时间】:2021-09-11 03:16:57
【问题描述】:

大家好,

我正在使用 python-pptx 模块生成包含一些数据的表。表格按预期输出数据,但由于字体的原因,它只是从各个方向滑出。

我正在尝试减小表格中文本的字体大小以使其适合幻灯片,

python-pptx 文档确实提到它可以做到,但只强调形状,而不是表格,

来自此处的文档,

https://python-pptx.readthedocs.io/en/latest/user/text.html#applying-character-formatting

是否意味着表格单元格也被视为形状?

如果是这样,我如何在底部的代码中使用下面的相同示例。感谢您提供这方面的任何帮助。

文档中的片段

===========================

应用文本框架级格式

下面会生成一个带有单个段落的形状,底部比顶部边距稍宽(这些默认为 0.05 英寸),没有左边距,文本顶部对齐,并且自动换行关闭。此外,自动调整大小行为被设置为调整形状的宽度和高度以适合其文本。请注意,垂直对齐是在文本框架上设置的。每个段落都设置了水平对齐方式:

from pptx.util import Inches
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE

text_frame = shape.text_frame
text_frame.text = 'Spam, eggs, and spam'
text_frame.margin_bottom = Inches(0.08)
text_frame.margin_left = 0
text_frame.vertical_anchor = MSO_ANCHOR.TOP
text_frame.word_wrap = False
text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT

TextFrame.auto_size 和 TextFrame.vertical_anchor 的可能值分别由枚举 MSO_AUTO_SIZE 和 MSO_VERTICAL_ANCHOR 指定。

================================================ ==========================

我的密码:

import pandas as pd
from pptx import Presentation
from pptx.util import Inches
from PIL import Image

prs = Presentation()
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes

data =pd.read_csv('dest_file2.csv', sep=',',skiprows=[0],nrows=11)
shapes.title.text = 'Inbound Traffic'
rows = 6
cols = 7
left = top = Inches(2.0)
width = Inches(6.0)
height = Inches(0.8)

table = shapes.add_table(rows, cols, left, top, width, height).table

# set column widths
table.columns[0].width = Inches(2.0)
table.columns[1].width = Inches(1.0)

# write column headings
table.cell(0, 0).text = 'Server'
table.cell(0, 1).text = 'Client'
table.cell(0, 2).text = 'Port'
table.cell(0, 3).text = 'Avg Bits/s'
table.cell(0, 4).text = 'Avg Packets/s'
table.cell(0, 5).text = 'Total Bytes'
table.cell(0, 6).text = 'Total Packets'

print(data)

a = data['Server']
b = data['Client']
c = data['Port']
d = data['Avg Bits/s']
e = data['Avg Packets/s']
f = data['Total Bytes']
g = data['Total Packets']
i=1
j=0
size = len(a)-1
while j < size:
    # write body cells
    table.cell(i, 0).text = a[j]
    table.cell(i, 1).text = b[j]
    table.cell(i, 2).text = c[j]
    table.cell(i, 3).text = str(d[j])
    table.cell(i, 4).text = str(e[j])
    table.cell(i, 5).text = str(f[j])
    table.cell(i, 6).text = str(g[j])
    print(a[j],b[j],c[j],d[j],e[j],f[j],g[j])
    i=i+1
    j=j+1

prs.save('test.pptx')

【问题讨论】:

    标签: python python-3.x python-pptx


    【解决方案1】:

    您需要知道表格单元格包含文本框架,其中包含包含运行的段落。运行有一个字体对象,您可以在那里指定字体的大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-03
      • 2017-02-24
      • 2018-12-20
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2023-02-25
      • 2015-02-13
      相关资源
      最近更新 更多