xlsxwriter code (in python) not freezing top row - xlsxwriter

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

Related

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.

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

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()

Running Macro on a WokSheet of Excel

I am able to create a excel from Python win32com ,so i know all the details of excel viz. Total number or rows , columns , Worksheet Names and location where it is saved. Now i want to run some application which can run a macro on a worksheet of created excel .
Example of macro :
Sub Macro2()
'
' Macro2 Macro
'
'
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=Range("C2:C6")
End Sub
Please tell which application can do this .
Well as you are probably aware, a little bit of work might be involved, but xlwings should be right up your street. For example from within VBA we can import functions by calling Python. From the docs:
Sub MyMacro()
RunPython ("import mymodule; mymodule.rand_numbers()")
End Sub
#which will run the following or any required, saved function.
import numpy as np
from xlwings import Workbook, Range
def rand_numbers():
wb = Workbook.caller() # Creates a reference to the calling Excel file
n = Range('Sheet1', 'B1').value # Write desired dimensions into Cell B1
rand_num = np.random.randn(n, n)
Range('Sheet1', 'C3').value = rand_num
Enjoy, http://xlwings.org/