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.
Related
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()
I am trying to read excel (.xlsx) file and convert it to dataframe. I used pandas.ExelFile , pandas.read_excel, openpyxl load_workbook and even io file reading methods but i am unable to read Sheet of this file. Every time i get list index out of range error or no sheet names is case of openpyxl. Also tried xlrd method.
temp_df = pd.read_excel("v2s.xlsx", sheet_name = 0)
or
temp_df = pd.read_excel("v2s.xlsx", sheet_name = "Sheet1")
or
from openpyxl import load_workbook
workbook = load_workbook(filename="v2s.xlsx",read_only = True, data_only = True)
workbook.sheetnames
Link to excel file
According to this ticket, the file is saved in a "slightly defective" format.
The user posted that he used Save As to change the type of document back to a normal Excel spreadsheet file.
Your file is this type:
You need to save it as:
Then running your code
from openpyxl import load_workbook
workbook = load_workbook(filename="v2s_0.xlsx",read_only = True, data_only = True)
print(workbook.sheetnames)
Outputs:
['Sheet1']
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)
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')
I went through these link and other links for xlwt, xlrd and xlutils
writing to existing workbook using xlwt
from xlutils.copy import copy
from xlrd import open_workbook
from xlwt import easyxf
rb = open_workbook('example.xls',formatting_info=True)
r_sheet = rb.sheet_by_index(0)
print r_sheet
print r_sheet.name
wb = copy(rb)
w_sheet = wb.get_sheet(0)
print w_sheet.name
w_sheet.save('example.html') #it throws an error in the last line saying "AttributeError: 'Worksheet' object has no attribute 'save'"
How can I save the worksheet alone as HTML file uing python
I am not sure you can save and excel sheet as html just with xlwt or xlrd.
One alternative is to use pandas which internally uses xlrd and xlwt saving you of coding all the steps involved.
You can read your excel sheet with
df = pandas.read_excel('example.xls', sheetname='sheet1')
and get it as html with:
html = df.to_html()
html is a string you can save in a file with open(myfile, 'w').write(html)