Python - Hide a worksheet - python

I am following the code to hide a worksheet using xlsxwriter. Anyone knows why this is failing?
https://xlsxwriter.readthedocs.io/example_hide_sheet.html
import xlsxwriter
import pandas as pd
file = 'C:/Prueba/GOOG.xlsx'
workbook = xlsxwriter.Workbook(file)
worksheet = workbook.get_worksheet_by_name('Sheet1')
worksheet.hide()
workbook.close()
I am getting the following error:
AttributeError: 'NoneType' object has no attribute 'hide'
The Excel has two worksheets: "Sheet1" and "Sheet2"

You have not set the column of your worksheet by worksheet.set_column()
Here is the correct code
import xlsxwriter
import pandas as pd
file = 'C:/Prueba/GOOG.xlsx'
workbook = xlsxwriter.Workbook(file)
worksheet = workbook.get_worksheet_by_name('Sheet1')
worksheet.set_column('A:A',30)
worksheet.hide()
workbook.close()

Related

python - pandas with openpyxl damages my workbook

Using pandas with the openpyxl engine I want to open an excel woorkbook and add a sheet.
Because, the sheets are growing and getting quit big I want to create a new sheet without reading all the other sheet.
So instead of xlsxwriter, I did start using openpyxl, however I get the following issue that the file gets damaged and needs to recovered by excel.
After that, when I run it a second time, I get the python error that the excel file is 'raise BadZipFile("File is not a zip file")'.
This my test code:
import openpyxl
import pandas
from pandas import ExcelWriter
from pandas import ExcelFile
from openpyxl import Workbook
from openpyxl import load_workbook
sheet1 = 'sheet1'
sheet2 = 'sheet2'
if os.path.exists(filename):
workbook = openpyxl.load_workbook(filename)
else:
workbook = Workbook()
writer = pandas.ExcelWriter(filename, engine='openpyxl')
writer.book = workbook
df_1.to_excel(writer, sheet_name=sheet1)
writer.save()
writer.close()
time.sleep(2) # to give it some time. But doesn't help :(
if os.path.exists(filename):
workbook = openpyxl.load_workbook(filename)
else:
workbook = Workbook()
writer = pandas.ExcelWriter(filename, engine='openpyxl')
writer.book = workbook
df_2.to_excel(writer, sheet_name=sheet2)
writer.save()
writer.close()
Any suggestions how to solve this? Or do I miss something?
btw, excel 365 - 16.46 macOS

Openpyxl is not saving the excel file, why?

here is my code:
import openpyxl as op
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl import worksheet
path = ...
op.load_workbook(path)
wb = Workbook()
ws2 = wb.create_sheet('Sheet2')
wb.save(filename = 'NameOfTheFile.xlsx')
Now everything works, except saving the file, when I add:
print(wb.sheetnames)
it does print out the Sheet2 I just added, but for some reason when I go to the documents where the file is, it does not update it, that sheet is nowhere to be found.

Open and modify specific worksheet in excel workbook

I am trying to open and format a specific Excel worksheet. However I am having trouble trying to find out how to look at a specific worksheet.
The code I'm trying to use to open the workbook and go to a specific worksheet and then change the font for specific cells is:
from openpyxl import Workbook
def applyValidations(path,tabname):
workbook = Workbook(path)
worksheet = workbook[tabname]
c = worksheet['A1:A5']
c.font = Font(size=22)
The error I'm getting is:
KeyError: 'Worksheet Department Data does not exist.'
Department Data is the name of the worksheet which does exist in the workbook.
Here's some code I use regularly with openpyxl, which may solve your Q:
from openpyxl import load_workbook
wb = load_workbook(filename=data_file, read_only=True)
ws = wb.active
print(wb.sheetnames)

There's an "AttributeError: 'Sheet' object has no attribute 'write'"

I'm trying to write a value into an excel document and it gives me an error.
This is on Pycharm and I want it to write a value if there is nothing there
import xlrd
import xlwt
import xlsxwriter
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
if sheet.cell(4,4).value == xlrd.empty_cell.value:
sheet.write(4,4, '120')
I'd expect it to just write on the excel document but it isn't working.

Insert image in existing excel sheet by using xlsxwriter

i am trying to add image into the existing excel sheet by using xlsxwriter module
import xlsxwriter
workbook = xlsxwriter.Workbook('C:/Users/Desktop/blank.xlsx')
worksheet = workbook.get_worksheet_by_name('Sheet1')
worksheet.insert_image('B5', 'C:/Users/Desktop/CaseDeatails/Abc.jpg')
i am getting the below error
Traceback (most recent call last):
File "C:\Users\Desktop\insertImage.py", line 23, in
worksheet.insert_image('B5', 'C:/Users/Desktop/CaseDeatails/Abc.jpg')
AttributeError: 'NoneType' object has no attribute 'insert_image'
Please help me on this error
inseart image in xlsxwriter
import xlsxwriter
import os
workbook = xlsxwriter.Workbook('C:/Users/Desktop/blank.xlsx')
worksheet = workbook.get_worksheet_by_name('Sheet1')
image = os.path.join(settings.BASE_DIR, "C:/Users/Desktop/CaseDeatails/", "Abc.jpg")
worksheet.insert_image('B5', image)
That isn't possible with XlsxWriter since it cannot read or modify an existing file.
Try the OpenPyXL module instead.
Also for some reason python doesn't like the links to be:
C:/Users/Desktop/blank.xlsx
They have to have a double /, so it should be:
C://Users//Desktop//blank.xlsx
It is not a xlsxwriter solution, but it works well:
from openpyxl import Workbook
from openpyxl.drawing.image import Image
wb = Workbook()
sheet1 = wb.create_sheet('sheet1',0)
active = wb['sheet1']
active.add_image(Image('fig.png'),'A1')
wb.save('myfile.xlsx')

Categories