I'm trying to use xlwings for the first time but I can't quite understand the documentation.
From the section "Quickstart" I read
import xlwings as xw
wb = xw.Book() # this will create a new workbook
When I try this "at home", I have no problem importing xlwings but with the second script I get this error:
AttributeError: module 'xlwings' has no attribute 'Book'
When I try to see the attributes of xw I can see AboveBelow,ActionTime etc. but not Book.
Can you help please?
Thank you very much.
I had the error also and changed from Workbook to Book. It is a version issue.
wb = xw.Workbook()
This fixed the issue for me:
import xlwings as xw
wb = xw.book()
I opened the excel file and seen book written as Book(first capital letter), so I changed it in the code as below:
import xlwings as xw
wb = xw.Book()
I had the same issue... in my case it was the name of the file I used: "xlwings.py"
Do NOT use that name for the file or the containing folder or you'll get an error.
Related
I am trying to use a simple delete_rows line to clear some data our of a sheet after importing via openpyxl, but the function is unrecognized by my Visual Studio Code.
When I use from openpyxl import load_workbook, the load_workbook text is grayed out like it's not recognized, but my openpyxl.load_workbook line is working. For some reason when I type out delete_rows the function is not autofilled like it's not being recognized.
Tried pip install openpyxl, but I already have the latest.
delete_rows is a method of the Worksheet object (docs), not a function like load_workbook, so you do not need to import it, just call it on a Worksheet (see also this demo page).
from openpyxl import load_workbook
wb = load_workbook("<xlsx_file_name>")
ws = wb.active
ws.delete_rows(2, 1)
I am working on a python prtogram that reads an excel file and based on the information in that file, writes data in the same file
This is my code:
import xlrd
import xlwt
from xlutils import copy
location = "C:\\Users\\adarsh\\Desktop\\Python\\Other\\Blah.xls"
readbook = xlrd.open_workbook(location)
workbook = xlutils.copy(readbook)
sheet = workbook.get_sheet(0)
I get this error when I run my code:
workbook = xlutils.copy(readbook)
AttributeError: module 'xlutils' has no attribute 'copy'
There is an error saying that there is no attribute copy even though online tutorials use that feature
I don't know how to fix this
looks like you haven't imported the right function from the right place. Try this:
from xlutils.copy import copy
Then you can simply call:
copy(readbook)
You imported copy specifically from the module so you shouldn't need the xlutils.copy() it should just be copy()
I am starting on a code that loads and edits excel (the version I am using is office 2017) sheet using openpyxl. Right now I am still trying to wrap my head around how this module works, here's the code
import openpyxl
from openpyxl import load_workbook
from openpyxl import workbook
from openpyxl.compat import range
from openpyxl.utils import get_column_letter
import os
os.chdir("D:\Scripts\Python\Testing Scripts\My Excel Folder")
wb = load_workbook("MyExcel.xlsx")
names = wb.sheetnames()
print(names)
print(type(wb))
and the error I receive is,
TypeError: 'list' object is not callable
For the string of code
names = wb.sheetnames()
wb.get_sheet_names() returns the list of all the sheets in that excel workbook.
print (wb.get_sheet_names())
for the latest openpyxl to avoid warning:
print (wb.sheetnames)
if you want to access a particular sheet
ws = wb.get_sheet_by_name(name = 'Sheet 1')
Use: wb.sheetnames
Example -
names = wb.sheetnames
print(names)
Do not use: get_sheet_names()
If you will use this, you will get this Warning.
DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames).
I am trying to get a good method to see if an Excel spreadsheet exists, if it does use that, if not create a new excel file. See code snippet below. The weird thing is every time I run it, it crashed on first attempt. If I run it again, it cruises through. Any ideas why? I am thinking it has something to do with xlrd vs. xlwt, but haven't found a solution yet. All modules are up-to-date.
import pandas as pd
import xlsxwriter
from xlrd import open_workbook
import xlwt
import os.path
fname=r'testmonthlyz.xlsm'
fname2=r'testmonthlyoutput2.xlsx'
#workbook = xlsxwriter.Workbook(fname2)
if os.path.isfile(fname2):
print('old file')
book=open_workbook(fname2)
else:
print('new file')
book=xlwt.Workbook(fname2)
ws = book.add_sheet('Tested')
sheet_names=book.sheet_names()
I believe that the reason that is crashed is since when you are in the else section, you have the line book=xlwt.Workbook(fname2) which means the book type is Workbook which has no attribute called sheet_names().
When you are using book = open_workbook(fname2) inside the if, book type is Book which does have sheet_names() attribute.
my solution to this, even though is's not the best way, but I think it will solve the issue you are dealing with..
change the following lines
import pandas as pd
import xlsxwriter
from xlrd import open_workbook
import xlwt
import os.path
fname=r'testmonthlyz.xlsm'
fname2=r'testmonthlyoutput2.xlsx'
workbook = xlsxwriter.Workbook(fname2)
if os.path.isfile(fname2):
print('old file')
book=open_workbook(fname2)
else:
print('new file')
workbook2=xlwt.Workbook(fname2)
ws = workbook2.add_sheet('Tested')
workbook2.save(fname2)
book = open_workbook(fname2)
sheet_names=book.sheet_names()
I have been trying to write some lists to a certain sheet on a workbook but am having no luck. My code is:
import xlwings as xw
from xlwings import Range
from xlwings import Book
wkb = xw.Book('Master_v3.xlsm')
sht = wkb.sheets['Control']
sht.Range('A1').value = some_list
This gives me the error:
*** AttributeError: 'Sheet' object has no attribute 'Range'
When I use "0" instead of specifying the sheet name (i.e. "Control") this seems to work. Where am I going wrong here?
Thanks
xw.Range is a shortcut for the Range on the active sheet of the active book of the active app.
When you fully qualify like you do, then range is an attribute of the sheet object, which follows the Python naming conventions of lower case for attributes:
sht.range('A1').value = some_list
I.e. mind xlwings.Range vs. mysheet.range.
Try this solution:
import xlwings
wb = xlwings.Book(r"filename")
wb.sheets['Sheet1'].range((5,1),(195,13)).value = ...