2017-06-01 228 views
0

我已经在pptx文件中插入了一个表格,并且我想自动调整列宽,以便列单元格中的任何文本都不需要断行。所以简单地说,我正在寻找整列的自动调整。使用python自动调整列宽pptx

我至今是:

from pptx import Presentation 
from pptx.util import Inches 
from PandasToPowerpoint import df_to_table 
import pandas as pd 

d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']), 
    'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])} 
df = pd.DataFrame(d) 
prs = Presentation() 
title_slide_layout = prs.slide_layouts[0] 
slide = prs.slides.add_slide(title_slide_layout) 

top = Inches(2.5) 
left = Inches(0.48) 
width = Inches(12.3) 
height = Inches(3.0) 
df_to_table(slide, df.reset_index(), left, top, width, height) 


for shape in slide.shapes: 
    if shape.has_table: 
     table = shape.table 
     for column in table.columns: 
      column.width=Inches(3) 

prs.save('test.pptx') 

我想:

column.text_frame.auto_size = True 
column.auto_size = True 
column.fit_text= True 

,但他们都没有工作。如果自动调整不起作用,我也可以遍历一列中的所有单元格,并确定最长文本的单元格,并将整列宽度调整为该文本。但我也无法找到正确的命令来获取文本宽度...任何想法是赞赏!

回答

1

我知道你正在寻找一个解决方案来调整列宽,但如何调整行高:

import pptx.enum.text MSO_AUTO_SIZE 

table.cell(row, column).text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT