Print 5 letter or numbers to fit in cells


Print 5 letter or numbers to fit in cells



I would to make a table. The cells have a fixed size.



How can I cut the letters or numbers so that they fit in the cells? Is there a possibility (e.g. limit string or int)?



It should run automatically automated and work for all sorts of scenarios.
This is my Code.


import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import pandas as pd
from pandas.plotting import table


def main():
data_frame = pd.read_excel('C:/test/TestArrays.xlsx')

with PdfPages('multipage.pdf') as pdf:
columns_per_page = 10
for page_no, start_column in enumerate(
range(0, data_frame.shape[1], columns_per_page), 1
):
sub_frame = data_frame.iloc[
0:15, start_column:start_column + columns_per_page
]
fig, ax = plt.subplots(figsize=(5, 1))
fig.suptitle('1.Excel-Daten-Seite-{}'.format(page_no), fontsize=15)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.set_frame_on(False)
a_table = table(ax, sub_frame, colWidths=[0.2] * sub_frame.shape[1])
a_table.auto_set_font_size(False)
a_table.set_fontsize(15)
a_table.scale(1.5, 1.5)
cells = a_table.get_celld()
for cell in (cells[0, i] for i in range(sub_frame.shape[1])):
cell.set_fontsize(13)
cell.set_color('grey')
pdf.savefig(fig, bbox_inches='tight', dpi=300)


if __name__ == '__main__':
main()



image





If s="mylongstring", you can limit the string to 5 characters via s[:min(len(s),5)]. The same does not work with integers in general, i.e. if your integer is 123456 and you want to cut it to 5 places, it gets completely wrong (12345). In that sense you need to define what you would like to happen in such case.
– ImportanceOfBeingErnest
Jul 2 at 10:51



s="mylongstring"


s[:min(len(s),5)]


123456


12345





s[:min(len(s), 5)] behaves the same way as s[:5], even if the string is shorter than 5 characters.
– user3483203
Jul 2 at 10:56



s[:min(len(s), 5)]


s[:5]





No, thats exactly what I want. Olny the first 5 numbers should be displayed. I need that for an overview.
– mgm841
Jul 2 at 10:58





And how does it work for at 100 columns or raws?
– mgm841
Jul 2 at 11:00





"{:.5}".format(str(val)) might work better for you depending on what you're trying to do.
– Matthew Story
Jul 2 at 11:02


"{:.5}".format(str(val))









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages