I'm trying to write a script to programmaticaly format the font, autofit columns width of an input.xlsx file which background is transparent (=no background color). Grid lines are visible when opened with gnumeric spreadsheet.
Till now, the python scripts to format the input.xlsx file are working well, excepted that their background is white. I have to open the input.xlsx file, select the whole sheet (Ctrl-A); then select the «background color» menu icon, and select ther «clear background» to get the grid lines visible.
Asking thei i.A. I got some codes attempting to delete backgrounds related codes,but without success.
example:
----------------------
wb = load_workbook("file.xlsx")
for ws in wb.worksheets:
# remove sheet background image (internal attribute used by openpyxl)
if hasattr(ws, "_background"):
ws._background = None
# clear tab color
ws.sheet_properties.tabColor = None
# optional: clear any per-cell fills (resets to no fill) If you only need to remove the sheet background image, omit the cell-fill loop.
for row in ws.iter_rows():
for cell in row:
cell.fill = PatternFill() # default empty fill
wb.save("file_no_bg.xlsx")
----------------------
or this one else:
-----------------------
from openpyxl import load_workbook
from openpyxl.styles import PatternFill
import zipfile
import os
infile = "in.xlsx"
outfile = "out_no_bg.xlsx"
wb = load_workbook(infile)
for ws in wb.worksheets:
# remove sheet background image (internal attr)
if hasattr(ws, "_background"):
ws._background = None
# clear tab color
ws.sheet_properties.tabColor = None
# optional: clear cell fills
for row in ws.iter_rows():
for cell in row:
cell.fill = PatternFill() # resets to no fill
wb.save(outfile)
-----------------------
It seems that a background comes embedded with openpyxl (mine is 3.0.9 version)
I'm wondering if the default openpyxl code could defaultly embed no background?
Thanks for your time and any workaround
Charlie