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 = ...
Related
I use excel as a front end and use python to pick up data from it and insert into sqlite db. once entered, i wish to clear selected fields from MS excel.
Here is the code i have been trying to use via xlwings:
xw.Range('A6:R50').table.clear_contents(). This is not working.
It is showing the following error:
NoneType' object has no attribute 'clear_contents'
First you have to specify a worksheet and you should use the right syntax. Here is a working example:
import xlwings as xw
wb = xw.Book("test.xlsx")
ws1 = wb.sheets['Sheet1']
ws1.range("A6:R50").clear_contents()
I'm using Python and openpyxl library, but, I'm not able to use the insert_cols() function in openpyxl when my spreadsheet is in write_only=True mode. So, basically, I just want to add a new column to my spreadsheet when it's in write_only=True mode.
I'm able to use insert_cols() when loading the workbook by load_workbook(), but, not when I'm using the write_only mode. I have to use the write_only mode because my spreadsheets are quite large.
Any ideas on how to add a new column are appreciated.
Thank you.
This is my code:
import openpyxl
from openpyxl import Workbook
from openpyxl import load_workbook
wb = load_workbook(filename=r'path\myExcel.xlsx', read_only=True)
ws = wb['PC Details']
wb_output = Workbook(write_only=True)
ws_output = wb_output.create_sheet(title='PC Details')
for row in ws.rows:
rowInCorrectFormat = [cell.value for cell in row]
ws_output.append(rowInCorrectFormat)
for cell in row:
print(cell.value)
### THIS IS THE PART OF THE CODE WHICH DOES NOT WORK
ws_output.insert_cols(12)
ws_output['L5'] = 'OK or NOT GOOD?'
###
wb_output.save(r'path\test_Output_optimized.xlsx')
This is the exact error that I'm getting:
ws_output.insert_cols(12)
AttributeError: 'WriteOnlyWorksheet' object has no attribute 'insert_cols'
The problem here lies in the flag write_only = True. Workbooks created by this flag set to true are different from regular Workbooks as you can look below.
Functions like insert_cols & insert_rows also do not work for such workbooks.
Possible solutions might be to not use this flag or use the ways suggested in the official documentation for adding data to the sheet.
For working with workbooks you might also find this article interesting. https://medium.com/aubergine-solutions/working-with-excel-sheets-in-python-using-openpyxl-4f9fd32de87f
You can read more in the official documentation. https://openpyxl.readthedocs.io/en/stable/optimized.html
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).
My goal is to read in an excel file and view the codes in a pandas dataframe (i.e. '= A3') rather than the resulting values from excel executing the codes, which is the pandas default if read in using pandas.
My goal was described here: How can I see the formulas of an excel spreadsheet in pandas / python?
Openpyxl is supposed to support this, but I can't get the import to function correctly. Anyone spot the error?
import pandas as pd
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
df = pd.DataFrame()
wb = load_workbook(filename = 'name.xlsx')
sheet_names = wb.get_sheet_names()
name = sheet_names[0]
sheet_ranges = wb[name]
df = pd.DataFrame(sheet_ranges.values)
> AttributeError: 'Worksheet' object has no attribute 'values'
(Note: the exact implementation of the answer at the linked question yields KeyError: 'Worksheet range names does not exist.' My code above resolved this, but then gets stuck as described.)
Check your version of openpyxl, It seems you have an older version.
openpyxl 2.4.2
import openpyxl
print(openpyxl.__version__)
Values property for worksheets were added only from 2.4.0-a1 (2016-04-11)
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.