1

在此处输入图像描述

我遇到了无法格式化表格的问题。文本太长,无法仅编辑尺寸或文本大小。如何快速更改此设置,以便在我为每列填充更多数据时查看所有文本?我正在寻找一种换行文本的功能,但我不知道我这样做是否可行。您还有其他方法可以推荐吗?我将表格更改为 .png 以插入 Excel 文件。它必须是 .png,因此它是一个对象,并且不会影响 Excel 中行和列的大小。

import matplotlib.pyplot as plt 
import xlsxwriter as xl
import numpy as np
import yfinance as yf
import pandas as pd 
import datetime as dt
import mplfinance as mpf 
import pandas_datareader 
from pandas_datareader import data as pdr
yf.pdr_override()
import numpy as np

Individualreport = "C:\\Users\\Ashley\\FromPython.xlsx"
Ticklist = pd.read_excel("C:\\Users\\Ashley\\Eyes Trial Data Center.xlsx",sheet_name='Tickers', header=None)
stocks = Ticklist.values.ravel()
PipelineData = pd.read_excel("C:\\Users\\Ashley\\Eyes Trial Data Center.xlsx", sheet_name='Pipeline', header=None)
writer = pd.ExcelWriter(Individualreport, engine='xlsxwriter')

for i in stocks:

    #write pipeline data
    t = PipelineData.loc[(PipelineData[0]==i)]
    print(t)
   
    def render_mpl_table(data, col_width=10, row_height=1, font_size=10, wrap=True,
                     header_color='#40466e', row_colors=['#f1f1f2', 'w'], edge_color='w',
                     bbox=[0, 0, 1, 1], header_columns=0,
                     ax=None, **kwargs):
        if ax is None:
            size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array([col_width, row_height])
            fig, ax = plt.subplots(figsize=size)
            ax.axis('off')
        mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, **kwargs)
        mpl_table.auto_set_font_size(False)
        #mpl_table.set_fontsize(font_size)

        for k, cell in mpl_table._cells.items():
            cell.set_edgecolor(edge_color)
            if k[0] == 0 or k[1] < header_columns:
                cell.set_text_props(weight='bold', color='w')
                cell.set_facecolor(header_color)
            else:
                cell.set_facecolor(row_colors[k[0]%len(row_colors) ])
        return ax.get_figure(), ax


    fig,ax = render_mpl_table(t, header_columns=0, col_width=2.0)
    fig.savefig(str(i)+'pipe.png')
4

1 回答 1

1

我想我需要使用一个额外的包,没有尝试过这个例子,但在另一个类似的例子中工作过。

from textwrap import wrap
label = ("label text that is getting put in the graph")
label = [ '\n'.join(wrap(l, 20)) for l in label ] 

#20 is number of characters per line

于 2021-05-26T22:03:29.560 回答