Setting custom header on each page of an excel sheet using xlsxwriter - header

Is it possible to set custom header on each page within the same sheet in excel using xlsxwriter?
Here I get two pages, but the same header on each. So this just sets for the entire sheet.
import xlsxwriter
data = ["a", "b", "c"]
wb = xlsxwriter.Workbook('test.xlsx')
ws = wb.add_worksheet()
for index,d in enumerate(data):
ws.write(index,0,d)
ws.print_area(index,0,index,10)
ws.set_header("&C Header %s"%d)
ws.set_h_pagebreaks([index])
ws.set_page_view()
wb.close()

Related

xlsxwriter code (in python) not freezing top row

I am using the below code xlsxwriter code to freeze the top row of my excel sheet. However the code is not freezing the top row at all. Any ideas?
import xlsxwriter
...
workbook = xlsxwriter.Workbook(XLSX_FILE)
currentWorksheet = workbook.get_worksheet_by_name(k)
currentWorksheet.write("A1", "User ID")
currentWorksheet.write("B1", "Name")
currentWorksheet.write("C1", "Email")
currentWorksheet.write("D1", "Mobile App access")
currentWorksheet.write("E1", "Dashboard access")
currentWorksheet.write("F1", "Registrations complete")
currentWorksheet.freeze_panes(1, 0) # Freeze the Header Row

Can I find and apply PatternFill to all blank (empty cells) in a worksheet with openpxl

Newbie! Please be gentle.
I'm trying to save a worksheet using openpyxl with some formatting.
I have blank/empty cells in my worksheet and I would like to format/fill them.
So. Find all blank/empty cells
Apply PatternFill
import openpyxl
from openpyxl import Workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
from openpyxl.styles import Fill, Color
catalogwb = openpyxl.load_workbook(currentDir + fileName)
catalogws = catalogwb.active
catalogws1 = catalogwb.create_sheet("Sheet2")
testcell = catalogws['B2']
testcell.fill = PatternFill("solid", fgColor="DDDDDD") #works!
max_row=catalogws.max_row
max_col=catalogws.max_column
print(max_row , max_col)
catalogws.column_dimensions['B'].width = 80
myFill = PatternFill("solid", fgColor="DDDDDD")
catalogws.cell(row=max_row,column=max_col).fill = myFill #works!
Spent too much time on this.
Thanks!

How to make Pandas Excel writer append to an existing sheet in a workbook instead of creating a new worksheet?

I have a excel workbook with two sheets ('variable','fixed'). In parallel, I have a data frame (pandas) with some data. I want to append the data in the data frame to the sheet ('variable') below the existing data in that sheet. But, the following code creates a new sheet called 'variable1' and dumps the data instead of appending to sheet 'variable'.
path = "data.xlsx"
book = load_workbook(path)
writer = pd.ExcelWriter(path, engine='openpyxl', mode='a')
writer.book = book
df3.to_excel(writer, sheet_name="variable",startrow=9,
index=False,header=False)
writer.save()
writer.close()
I have tried the above code. df3 is my pandas dataframe. I want my data to be pasted from row 9 as the existing data is until row 8 in sheet 'variable'. The code creates a new sheet ('variable1') and dumps data from row 9. I want it to paste the info in sheet ('variable') and not create a new one.
Can someone help me understand this dynamic?
To append an excel document use:
# open the workbook
wb = openpyxl.load_workbook('file_name.xlsx')
# assign a var to worksheet
ws = wb['sheet_name']
Then you can do things like:
# write to cell
wb['sheet_name'].cell(row=4, column=1).value = string_to_enter_to_cell
# format cells
for row in ws['A4:L4']:
for cell in row:
cell.value = None
cell.border = no_border

I am trying to switch every empty cell to one filled with the number 1

Hi I am currently using openpyxl and am trying to swap every empty cell to one with the number 1, and then eventually one filled in with red. Here is my code so far:
import openpyxl
from openpyxl.styles import Color, PatternFill, Font, Border
from openpyxl.utils import column_index_from_string
#redFill = PatternFill(fill_type=None, start_color='F2DCDB', end_color='F2DCDB')
wb = openpyxl.load_workbook('MRR.xlsx')
sheet = wb["Monthly MRR "]
for r in range(7, 793):
for col in range(4,21):
current_cell = sheet.cell(row=r, column=col).value
if current_cell == None:
current_cell = "LL"
wb.save("updatedMRR.xlsx")
Even though I loop through every item, it does not seem to change any of the cells.
You should change your code so that you refer to the actual cell and explicitly check and set its value. Your code currently checks a copy of the value and then overwrites the copy but not the actual cell.
current_cell = sheet.cell(row=r, column=col)
if current_cell.value is None:
current_cell.value = "LL"
PS use ws.iter_rows() rather than your own counter.

apache poi: change the font of existing entire sheet

I has a existing workbook containing multiple sheets. I have a sheet which contains some bold cells in it. I want to change the font of this entire sheet retaining the boldness of some of the cells. Can you please provide me the code snippet?
You will have to create more than one style for the workbook:
XSSFWorkbook wb = new XSSFWorkbook();
XSSFCellStyle fontStyleBold = wb.createCellStyle();
XSSFCellStyle fontStyleNormal = wb.createCellStyle();
and assign the cells you want.