Python Docx Table row height - python-3.x

So column width is done using cell width on all cells in one column ike this:
from docx import Document
from docx.shared import Cm
file = /path/to/file/
doc = Document(file)
table = doc.add_table(4,2)
for cell in table.columns[0].cells:
cell.width = Cm(1.85)
however, the row height is done using rows, but I can't remember how I did it last week.
Now I managed to find a way to reference the rows in a table, but can't seem to get back to that way. It is possible to change the height by using the add_row method, but you can't create a table with no rows, so the the top row will always be the default height, which about 1.6cms.
There is a way to access paragraphs without using add_paragraph, does anyone know how to access the rows without using the add_row method because it was that that I used to set row height in a table as a default.
I have tried this but it doesn't work:
row = table.rows
row.height = Cm(0.7)
but although this does not give an error, it also has no effect on the height.

table.rows is a collection, in particular a sequence, so you need to access each row separately:
for row in table.rows:
row.height = Cm(0.7)
Also check out row.height_rule for some related behaviors you have access to:
https://python-docx.readthedocs.io/en/latest/api/table.html#row-objects
When you assign to table.rows.height, it just adds a .height instance attribute that does nothing. It's one of the side-effects of a dynamic language like Python that you encounter a mysterious behavior like this. It goes away as you gain more experience though, at least it has for me :)

Some additional information:
The answer here is correct, but this will give a minimum row height. Using WD_ROW_HEIGHT_RULE.EXACTLY will fix the cell height to the set row height ignoring the contents of the cell, this can result in cropping of the text in an undesirable way.
para = table.cell(0,0).add_paragrph('some text')
SOLUTION:
add_paragraph actually adds a blank line above the text.
Use the following instead to avoid using add_paragraph:
table.cell(0,0).paragraphs[0].text = 'some text'
or using add_run can make it easier to also work with the text:
run = table.cell(0,0).paragraphs[0].add_run('some text')
run.bold = True

Related

Increasing height merged cells Apache POI

I am using Apache POI SXSSF to generate xlsx document. The document uses Times New Roman sizes 9 and 11, and the default cell width and height have been changed. The question is how to calculate the height of the merged cells so that all the text fits (the height of the cell must be dynamically set according to the given text)? The server running the application does not have a display, and this code is running in the IBM Integration Bus.
The solution from How to get the needed height of a multi line rich-text field (any font, any font size) having defined width using Java? is not suitable. The server running the application is missing a display and the string int ppi = java.awt.Toolkit.getDefaultToolkit().getScreenResolution(); returns an exception, and manually picking the ppi value is also not possible. If there is a display, everything works correctly.
And is there any way to use the "align center selection" function somehow?
I found that centering a selection gives a similar result as merging multiple cells horizontally, but I couldn't find an answer anywhere on how to use this in Apache POI. As a result, experimentally, I found out that in order to achieve this effect, you need to do the following things:
Create CellStyle; specify setWrapText(true) and setAlignment(HorizontalAlignment.CENTER_SELECTION) for it
Apply the style created in step 1 to all cells that need to be merged
Specify the value in the first cell
Code example:
Font font = wb.createFont(); // where wb - is SXSSFWorkbook object
font.setFontName("Times New Roman");
font.setFontHeightInPoints((short) 11);
CellStyle style = wb.createCellStyle();
style.setFont(font);
style.setWrapText(true);
style.setAlignment(HorizontalAlignment.CENTER_SELECTION);
for (int i = 0; i <= endCellNum - firstCellNum; i++){ // where endCellNum - number of last cell of selection and firstCellNum is number of first cell of selection
Cell cell = curRow.createCell(firstCellNum + i);
cell.setCellStyle(cs);
if (i == 0){
firstCell = cell;
}
}
firstCell.setCellValue(value);

Tkinter both 'sticky' and 'rowconfigure' did not fill the empty space

I have 2 frames inside root which called "header_frame" and "activity_frame" both are in the same column which is "column=0". I want both frames to be resizeable matching its root parent filling all empty space like this :
I have tried all grid config possibilities including setting 'rowconfigure' on 'root', set activity_frame to stick on "North" which is header_frame, last but not least I've also tried to stick header_frame to south which is the result I do not want because those frame share same size (I hope header_frame has 'maxsize' attribute but sadly it didn't have). so this is the code that I've tried :
self.root = root
self.column = ""
self.search = ""
self.root.minsize(500,480)
self.comboboxValue = None
self.root.title("CUCI (CSV Unique Column Identifier)")
self.root.configure(background="powder blue")
self.root.grid_columnconfigure(0,weight=1)
self.root.grid_rowconfigure(0,weight=1)
self.root.grid_rowconfigure(1,weight=1)
#header frame
self.header_frame = tk.Frame(self.root)
self.header_frame.grid(row=0, column=0,sticky="NEW")
self.header_frame.grid_columnconfigure(0,weight=1)
self.header_frame.grid_rowconfigure(0,weight=1)
self.header_frame.configure(background="grey")
#activity Frame
self.activity_frame = tk.Frame(self.root)
self.activity_frame.grid(row=1, column=0,sticky="NEWS")
self.activity_frame.grid_columnconfigure(0,weight=1)
self.activity_frame.grid_rowconfigure(0,weight=1)
self.activity_frame.configure(background="grey",pady=1)
Here's the layout result from my code which I do not expect:
The point is that I want to fill those empty spaces with activity_frame to be stick-ed on 'header_frame'.Please I do not wish to use pack(self.activity_frame.pack(fill=tk.X)). I just want to use grid because it's easy to use
The reason for the gap is because you don't have the header frame stick to the bottom of the space it was given. If you change the sticky attribute for the header to be "nsew" you'll see that the header fills the extra space.
self.header_frame.grid(row=0, column=0,sticky="nesw")
I'm guessing you don't want the header frame to be so tall. If that is the case, give row 0 a weight of 0 instead of 1. That way all extra unallocated space will be given to row 1.
self.root.grid_rowconfigure(0,weight=0)
After doing so, and after adding a couple of other widgets to simulate your screen, this is what it looks like:

Is there a way to change height of tkinter Treeview heading?

I got a problem with changing the height of the Treeview.heading. I have found some answers about the dimensions of Treeview.column, but when I access Treeview.heading in the documentation, there is not a single word about changing the height of the heading dynamically when the text doesn't fit (and wrapping it) or even just hard-coding height of the heading in pixels.
I don't have to split the text to two rows, but when I just keep it that long the whole table (as it has many entries) takes up the whole screen. I want to keep it smaller, therefore I need to split longer entries.
Here is how it looks like:
I can't find any documentation to verify this but it looks like the height of the heading is determined by the heading in the first column.
Reproducing the problem
col_list = ('Name', 'Three\nLine\nHeader', 'Two\nline')
tree = Treeview(parent, columns=col_list[1:])
ix = -1
for col in col_list:
ix += 1
tree.heading(f'#{ix}', text=col)
The fix
col_list = ('Name\n\n', 'Three\nLine\nHeader', 'Two\nline')
or, if you want to make it look prettier
col_list = ('\nName\n', 'Three\nLine\nHeader', 'Two\nline')
The only problem is I haven't figured out how to centre the heading on a two line header
Edit
The newlines work if it is the top level window but not if it is a dialog. Another way of doing this is to set the style. I've got no idea why this works.
style = ttk.Style()
style.configure('Treeview.Heading', foreground='black')
you can use font size to increase the header height for sometimes;
style = ttk.Style()
style.configure('Treeview.Heading', foreground='black', background='white', font=('Arial',25),)

Conditionally Color Table Cells with ReportLab

I've created a table using ReportLab. I would like to conditionally color the cells, depending on their contents (in my case, I want negative numbers to be red). To be clear, I have the conditional code working, I can't figure out how to add color. What I've tried:
using the <font color="..."> tag. Instead, the tags is included verbatim in the output.
wrapping each cell in Paragraph(...) (suggested in this answer). In this case, the cell text is linewrapped after each letter.
wrapping the table in Paragraph(...). In this case, reportlab errors out (I believe the resulting error was TypeError: split() missing required positional argument: 'availHeight')
I found reportlab.platypus.tables.CellStyle in the reportlab source code, but can't figure out make use of it. Google turns up nothing useful and it's not mentioned in the reportlab documentation.
I guess TableStyle(...) rules could be used, but the cells aren't in a predetermined position within the table (which is what all the examples assume).
Help appreciated!
Using TableStyle() would be an acceptable solution. You could loop through the data and add a style command when the condition is met.
Here is an example:
import random
from reportlab.lib.pagesizes import letter
from reportlab.lib.colors import red
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
# Generate random data with positive and negative values as list of lists.
data = []
for _ in range(20):
data.append(random.sample(range(-10, 10), 5))
table_style = TableStyle([('ALIGN', (0, 0), (-1, -1), 'RIGHT')])
# Loop through list of lists creating styles for cells with negative value.
for row, values, in enumerate(data):
for column, value in enumerate(values):
if value < 0:
table_style.add('TEXTCOLOR', (column, row), (column, row), red)
table = Table(data)
table.setStyle(table_style)
pdf = SimpleDocTemplate('example.pdf', pagesize=letter)
pdf.build([table])

How to encircle Invalid data in Excel?

I want to show a circle around only invalid data.
i have done the complete steps shown in this link
But this circle shown is very big and covers the entire cell.
I want a small circle only covering the data not the entire cell's width.
Data validation is a built in Excel functionality. It checks whole cell value.
So it is not possible, using Data validation, to accomplish what your trying.
It MAY BE POSSIBLE using VBA, shapes, events and (hard) parsing character rendering. In your place, I would be glad with this very big circles!!! :)
I agree with #LS_dev. See this MS Article about changing data validation for printing. Try modifying it to loop through all your data validation and change the width and height.
You can probably do it with this part of the code by changing the width and height:
If Not c.Validation.Value Then
Set o = ActiveSheet.Shapes.AddShape(msoShapeOval, _
c.Left - 2, c.Top - 2, c.Width + 4, c.Height + 4)
o.Fill.Visible = msoFalse
o.Line.ForeColor.SchemeColor = 10
o.Line.Weight = 1.25

Resources