Python-docx - Center a cell content in an existing table after adding values - docx

I have a .docx template with an empty table, where I am adding values:
def manipulate_table():
table = doc.tables[0]
table.cell(0, 0).text = 'A'
table.cell(0, 1).text = 'B'
table.cell(0, 2).text = 'C'
table.cell(0, 3).text = 'D'
After adding these values, the the table attribute "Centered" is gone, which is standard behaviour.
How can I loop through my table and center all values again? I've already Googled, but found nothing helpful. E.g.: does not work:
for cell in ....????:
tc = cell._tc
tcPr = tc.get_or_add_tcPr()
tcVAlign = OxmlElement('w:vAlign')
tcVAlign.set(qn('w:val'), "center")
tcPr.append(tcVAlign)
I appreciate all your help.

The .text property on a cell completely replaces the text in the cell, including the paragraph(s) that were there before.
The "centered" attribute is on each paragraph, not on the cell. So you need to do something like:
from docx.enum.text import WD_ALIGN_PARAGRAPH
cell.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
to each of the "new" paragraphs (assigning to .text will leave you with exactly one in each cell).

Related

Avoid Wrapping Text only in first column of R datatable

I'm struggling with keeping text in one column of a datatable from wrapping.
I'd like to avoid wrapping text in the first column (as it's the only part that makes the row size bigger), but keep the option in the headers (to avoid having to scroll).
I've tried adjusting the width of the first column, but the text keeps wrapping no matter what size I use.
DT::datatable(chartfilter,
rownames = FALSE,
options=list(iDisplayLength=7,
bPaginate=FALSE,
bLengthChange=FALSE,
bFilter=FALSE,
bInfo=FALSE,
rowid = FALSE,
autoWidth = FALSE,
ordering = FALSE,
scrollX = TRUE,
columnDefs = list(list(width='500px', targets = list(1)))
I've also found a solution that turns text wrapping off in the entire table - but I don't want that for my column labels. Adding this in the UI in front of the tableoutput:
tags$style(HTML("#charttable {white-space: nowrap; }")),
Is this possible, or do I just have to accept wrapping text in the first column? Appreciate any help I can get, and let me know if more info is needed.
Use the formatStyle() function to apply a specific style to a column:
datatable() %>% formatStyle("Region","white-space"="nowrap")
This function comes from the same library (DT). More info on formatStyle() can be found here: https://rstudio.github.io/DT/010-style.html

How to align 2 paragraphs side by side using Novacode DocX?

Using DocX I need to do something like this:
A long
text description.
Except I need the "1." to be a paragraph and the "A long text description" to be a paragraph. I cannot use a list for this.
late answer, but use a 2 column table with no visible borders
some code i hacked out of my current thing. this makes a table and populates the cells with the contents of my cells,
you'd just need to make a table, with 2 columns. and then iterate build a row with with cell[0] for the numeral, and cell[1] for the sentence
doc.InsertParagraph("Table Title: " + (component).SubTitle, false, SubtitleStyle);
var table = doc.AddTable(component.ColumnCount, component.RowCount);
int rowcount = 0;
foreach (DataCell datacell in component.TableData)
{
table.Rows[rowcount].Cells[0].Paragraphs.First().Append(rowcount+1);
table.Rows[rowcount].Cells[1].Paragraphs.First().Append(datacell.CellValue);
rowcount++;
}
doc.InsertTable(table);

itextsharp leaving first page empty and printing table conent in following table

I have three part in my PDF generating document using iTextSharp library.
The Header and footer I am printing using OnEndPage() of PdfPageEventHelper class. The header and footer printing well in all pages.But problem in printing large table content from middle of the page.
Now, I am facing problem when I have more that 100 table rows content to print on middle of the page.
On first page (middle of the page) I am printing table with more than 100 rows, but in this case it leave the first page empty and start printing table content on second page. Here is my table that I am trying to print on first page
PdfPTable table = new PdfPTable(5);
table.HeaderRows = 1;
table.SplitLate = false;
table.SplitRows = false;
table.SetTotalWidth(new float[] { 100, 75, 75, 75, 75 });
table.LockedWidth = true;
for (int i = 0; i < 150; i++)
{
PdfPCell cell = new PdfPCell(new phrase());
cell.Colspan = 5;
table.AddCell(cell);
}
I am using iTextSharp version: 5.4.5.0
Is there any configuration need to do, that will prevent page break?
But I am getting actual result in this format
Allow me to comment on these two lines:
table.SplitLate = false;
table.SplitRows = false;
The first line tells iTextSharp what it should do if a row doesn't fit a page. If you set this value to false, rows will be split late. For instance: you have a big row that doesn't fit the current page. In that case, the row will be forwarded to the next page. If it still doesn't fit the page, then another decision needs to be made: should the row be split or not?
The second line gives iTextSharp the answer to that question. If the value of SplitRows is true, then the row will be split. If the value is false, the row will be dropped. Allow me to quote the official documentation (p115 of "iText in Action - Second Edition"):
Ths is a dangerous line, because now not one row will be split. Rows that are too high to fit on a page will be dropped from the table!
I have the feeling that you want all the rows to show up in the table, so I suggest that you change the two lines I mentioned to:
table.SplitLate = true;
table.SplitRows = true;
Actually: why don't you just remove those two lines? The default value of SplitLate and of SplitRows is true.
Same thing happened to me using nested tables.
I solved this problem using .SplitLate = false; in the first table :)

avoid empty space in QTableView

In a QVBoxLayout added two QTableViews. The first one has constant number of rows (2), the second one has many. The goal is to make the second table begin just after last row in the first table.
I tried change QSizePolicy, setMinimumSize() - nothing helps. Here is sample of code:
layout = QVBoxLayout()
first_table = QTableView()
second_table = QTableVew()
first_table.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum)
second_table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
layout.addWidget(first_table)
layout.addWidget(second_table)
It looks like it uses sizeHint() no matter of setMinimumSize(h, v) values. How to make the first table do not show space without rows?
You might want to do the following:
layout = QVBoxLayout()
first_table = QTableView()
second_table = QTableVew()
layout.addWidget(first_table)
layout.addWidget(second_table)
layout.addStretch(1); // <--- to keep both tables together.

How to deal with Auto Width property for labels in Reports Devexpress?

Am designing simple report in Devexpress. I add three labels continuously in xtrareport. 1st label & last label are set in designer screen but middle label data comes from database it may be (big or small) word or sentence. If data comes from database big means 3rd label get overlap with this data. How to solve this ?? Thanks in advance.
0. Three labels.
In report designer set the width of you 2nd label as more as possible:
Set the CanShrink, CanGrow, Multiline and WordWrap to true:
xrLabel2.CanShrink = true;
xrLabel2.CanGrow = true;
xrLabel2.Multiline = true;
xrLabel2.WordWrap = true;
In result you will get something like this:
1. One label with special format.
You can use one label with format string. Use {0} as placeholder for your data source values:
xrLabel1.DataBindings.Add(new XRBinding("Text", null, "Value", "Here is the text: «{0}». Here is the end."));
Also you can set the CanShrink, CanGrow, Multiline and WordWrap to true.
The result will be something like this:
2. One label with [brackets] in the labels's text.
You can insert a data field name with [brackets] into the control's text. For example:
xrLabel1.Text = "Here is the text: «[Value]». Here is the end."
And here is the result:

Resources