XlsxWriter set_column limit rows - python

I have a pandas dataframe that I'm writing to a excel file with XlsxWriter. I'm setting the cell format with
worksheet.set_column(first_index, last_index, None, cell_format)
On a few of my columns. By doing this however not only the cells with values in my excel file gets the format applied, but seemingly infinite rows get the cell format applied.
How can I limit the cell format to a set of rows?

You can use conditional formatting:
worksheet.conditional_format(first_index, last_index, {'type': 'no_blanks',
'format': cell_format})
This works if the other rows are blank. I don't know if that's the case for you.

By doing this however not only the cells with values in my excel file gets the format applied, but seemingly infinite rows get the cell format applied.
That is how column formatting works in Excel.
How can I limit the cell format to a set of rows?
You can set row formatting with the set_row() method but from the overall question it sounds like you want to limit the formatting to a range of cells.
The only way to do that in Excel, or XlsxWriter, is to format the cells individually (apart from solutions like using conditional formatting or worksheet tables that can be applied to a range).
In order to do that with a dataframe you would need to avoid df.to_excel() and write the data cell by cell using XlsxWriter methods.

Related

pandas ExcelWriter merge but keep value that's already there

I have a few small data frames that I'm outputting to excel on one sheet. To make then fit better, I need to merge some cells in one table, but to write this in xlsx writer, I need to specify the data parameter. I want to keep the data that is already written in the left cell from using the to_excel() bit of code. Is there a way to do this without having to specify the data parameter? Or do I need to lookup the value in the dataframe to put in there.
For example:
df.to_excel(writer, 'sheet') gives similar to the following output:
Then I want to merge across C:D for this table without having to specify what data should be there (because it is already in column C), using something like:
worksheet.merge_range('C1:D1', cell_format = fmat) etc.
to get below:
Is this possible? Or will I need to lookup the values in the dataframe?
Is this possible? Or will I need to lookup the values in the dataframe?
You will need to lookup the data from the dataframe. There is no way in XlsxWriter to write formatting on top of existing data. The data and formatting need to be written at the same time (apart from Conditional Formatting which can't be used for merging anyway).

Pandas Excelwriter conditional formatting for percentage elements

I have a data frame like this: -
Now, if I want to format any particular excel sheet for percentage, the whole column gets formatted according to percentage and changes all values to percentage.
Is there any way to first search for strings that ends with '%' and then convert only that particular string to percentage format so that it could be accepted by excel and not yield the green error box in excel.
(I have tried conditional formatting and add_format method but it doesn't work)
Also, I am using pandas excelwriter for saving my dataframes into excel sheet, below is the code: -
Excelwriter = pd.ExcelWriter(Excel_File_Name,engine="xlsxwriter",engine_kwargs={'options': {'strings_to_numbers': True}})

Cell Format for a range of cells using xlsxwriter

I am using xlsxwriter to export pandas dataframe to excel file. I need format a range of cells without using worksheet.write function as the data is already present in cells.
If I am using set_row or set_column, it is adding the format to entire row or column.
Please help me find a solution.
I need format a range of cells without using worksheet.write function as the data is already present in cells.
In general that isn't possible with XlsxWriter. If you want to specify formatting for cells then you need to do it when you write the data to the cells.
There are some options which may or may not suit your needs:
Row and Column formatting. However that formats the rest of the row or column and not just the cells with data.
Add a table format via add_table().
Add a conditional format via conditional_format().
However, these are just workarounds. If you really need to format the cells then you will need to do it when using write().

Xlsxwriter, formatting just some cells, not whole row or whole column?

I have a dataframe that I am sending to Excel using the xlsxwriter engine, then applying formatting to a sheet before closing it. How can I only set a bg color for the columns that have data in them?
For example, I am able to add a yellow background color to the second column (below), but it extends past the cells that have data in them. I am doing this with set_column() as below:
worksheet.set_column(1, 1, 18, hilite_format)
I don't see an option to set column format for only certain rows, or to use set_row() to only format certain columns. I've already written the data to the worksheet, so I can't use worksheet.write() or it will overwrite the data that is already in there. At least I don't see any way to use .write() to just apply the formatting without writing data or None into the cells.
Is there a way to just 'paint' some formatting over a range of cells without affecting the values in those cells? I would prefer not to have to use conditional formatting or to re-write the data for these cells just to be able to get the formatting right.
Thank!
Try using the conditional_format(), highlighting the cells that are not blank:
worksheet.conditional_format('B:B', {'type': 'no_blanks',
'format': hilite_format})
Try this code :
You put your logic here and use whenever you want in cell.
merge_format = workbook.add_format({
'border': 1,
'align': 'center',
'valign': 'vcenter'})
worksheet.merge_range('A1:R1', 'AGENCIES', merge_format)

Pandas: how to format both rows and columns when exporting to Excel (row format takes precedence)?

I am using pandas and xlsxwriter to export and format a number of dataframes to Excel.
The xlsxwriter documentation mentions that:
http://xlsxwriter.readthedocs.io/worksheet.html?highlight=set_column
A row format takes precedence over a default column format
Precedence means that, if you format column B as percentage, and then row 2 as bold, cell B2 won't be bold and in % - it will be bold only, but not in %!
I have provided an example below. Is there a way around it? Maybe an engine other than xlsxwriter? Maybe some way to apply formatting after exporting the dataframes to Excel?
It makes no difference whether I format the row first and the columns later, or viceversa.
It's not shown in the example below, but in my code I export a number of dataframes, all with the same columns, to the same Excel sheet. The dataframes are the equivalent of an Excel Pivot table, with a 'total' row at the bottom. I'd like the header row and the total row to be bold, and each column to have a specific formatting depending on the data (%, thousands, millions, etc). Sample code below:
import pandas as pd
writer = pd.ExcelWriter('test.xlsx')
wk = writer.book.add_worksheet('Test')
fmt_bold = writer.book.add_format({'bold':True})
fmt_pct = writer.book.add_format({'num_format': '0.0%'})
wk.write(1,1,1)
wk.write(2,1,2)
wk.set_column(1,1, None, fmt_pct)
wk.set_row(1,None, fmt_bold)
writer.close()
As #jmcnamara notes openpyxl provides different formatting options because it allows you essentially to process a dataframe within a worksheet. NB. openpyxl does not support row or column formats.
The openpyxl dataframe_to_rows() function converts a dataframe to a generator of values, row by row allowing you to apply whatever formatting or additional processing you like.
In this case you will need to create another format that is a combination of the row and column formats and apply it to the cell.
In order to do that you will need to iterate over the data frame and call XlsxWriter directly, rather then using the Pandas-Excel interface.
Alternatively, you may be able to do using OpenPyXL as the pandas Excel engine. Recent versions of the Pandas interface added the ability to add formatting to the Excel data after writing the dataframe, when using OpenPyXL.

Categories

Resources