I'm using openpyxl (unfortunately I don't know how to find out my version number, installed it about a month ago) on Windows with python 2.7 and want to copy a worksheet that I generated using a template.xlsx file to a new workbook. The template has a single worksheet that I alter. I want to load it n times and copy each version as a new worksheet to another workbook. Could also be the same workbook ifneedbe.
I found some hints here which took me here. The example doesn't work as it seems the add_sheet() method has been removed.
primary.add_sheet(copy.deepcopy(ws),ido+1)
AttributeError: 'Workbook' object has no attribute 'add_sheet'
Also couldn't find anything helpful in the API.
I'm afraid copying worksheets is not supported because it is far from easy to do.
I was struggling with it as you. But, I could find out the way to solve.
The best way I think to copy Excel worksheets using Openpyxl and Python:
from openpyxl import Workbook, load_workbook
# workbook source = wb1 and workbook destination = wb2
wb1 = load_workbook('file.xlsx')
wb2 = Workbook()
ws1 = wbs.active
ws2 = wbd.active
for r in plan1.iter_rows():
for c in r:
ws2[c.coordinate] = c.value
wb2.save('file2.xlsx')
The FOR loop with iter_rows() creates a named list with existing filled cells. And the 2nd FOR iterates in those cells ('A1','A2','B1' etc). The method .coordinate can be applied to the cell(c) and extract the Column,Row like 'A1' as a string. If we add it as an index of the worksheet, we can set it as a variable. Then just get the value of the cell(c), the magic is done.
We can do something with data during the loop and after save it to the file.
Related
I have an excel workbook that has quite a few formulas, and when I try to upload the workbook into a database, the cells with iferror formulas come in as blanks even though it should be a string or number. I am new to python but I want to create a python file that will read in the sheet, and paste only the values into a new workbook.
I tried:
import openpyxl as xl
wb1 = xl.load_workbook('file1.xlsx')
ws1 = wb1["Sheet 1"]
wb2 = xl.load_workbook('file2.xlsx')
ws2 = wb2.create_sheet(ws1.title)
for row in ws1:
for cell in row:
ws2[cell.coordinate].value = cell.value
wb2.save('path')
The code works to copy the data into a new workbook, but it is pasting the formulas. I just want the values.
As per my earlier comment:
This comes from the OpenPyxl docs:
Where it's stated on the openpyxl.reader.excel.load_workbook submodule, looking at the data_only parameter:
data_only (bool) – controls whether cells with formulae have either the formula (default) or the value stored the last time Excel read the sheet
Default is Formulas whereas you want the values. So setting it to true:
wb = xl.load_workbook('file1.xlsx', data_only=True)
Should help :)
I want to add new records every week to this existing file without creating a new one.
For example, Next I want to add record on date 6/13/2016
Randy->(13,23,13)
Shaw->(13,15,13)
and many such entries next two months. How do I do that? I am beginner so having trouble to put it in syntax.
I could do only this much
import xlrd
#Opening the excel file
file_location= "C:/Users/agodgh1a/Desktop/Apurva/EPSON.xlsx"
workbook= xlrd.open_workbook(file_location)
sheet=workbook.sheet_by_index(0)
Thank you!
The lib you're using looks like it only reads, not edits. Here's an example in openpyxl:
from openpyxl import Workbook, load_workbook
# create the file
wb = Workbook()
ws = wb.active
ws.append([1, 2, 3])
wb.save("sample.xlsx")
# re-open and append
wb = load_workbook("sample.xlsx")
ws = wb.active
ws.append([4, 5, 6])
wb.save("sample.xlsx")
Run that and you'll have a file sample.xlsx with both rows.
xlrd
is for reading operations only. Since you want perform a write operation use
xlwt
python module.
Refer to xlwt docs for the same
I am trying to use openpyxl to:
Open an Excel (2016) workbook which contains 3 worksheets (Sheet1,Sheet2,Sheet3)
Remove a worksheet (Sheet2)
Save the workbook to a different workbook minus Sheet2
from openpyxl import load_workbook
wb = load_workbook("c:/Users/me/book1.xlsx")
ws = wb.get_sheet_by_name('Sheet2')
wb.remove_sheet(ws)
wb.save("c:/Users/me/book2.xlsx")
The wb.save will generate an IndexError: list index out of range error and produce a corrupted book2.xlsx file which Excel cannot open.
I run into similar problem, only with xlwt library. Regardless, the cause is the same, You remove the sheet which is set as active sheet. So, to fix this, before saving workbook, set some other sheet as active. In openpyxl, it would be something like this:
from openpyxl import load_workbook
wb = load_workbook("c:/Users/me/book1.xlsx")
ws = wb.get_sheet_by_name('Sheet2')
wb.remove_sheet(ws)
wb._active_sheet_index = 0
wb.save("c:/Users/me/book2.xlsx")
I must mention that this is not very good programming practice, but there is no method to set active sheet, only to get one.
EDIT: Just found out that this repo was moved to bitbucket, and found that it has method for setting active sheet. Just use:
wb.active = 0
Let me preface this by saying I have tried looking for, and cannot seem to find a similar situation so please don't be too upset if this seems familiar to you. I am using Python 2.7 and openpyxl version 2.2.5 (I need to use 2.7, and used an older module for other reasons.)
I am new to Python and read/write code in general, so I'm testing this on the command line before I implement it:
I created a file, foo.xlsx in the Python27 file directory with some values that I manually entered via Excel.
I then used this simple code on the Python command line to test my code
from openpyxl import load_workbook
wb = load_workbook('foo.xlsx')
sheet_ranges = wb['range names']
It then resulted in the following error:
File "C:\Python27\lib\openpyxl\workbook.workbook.py", line 233 in getitem
raise KeyError("Worksheet {0} does not exist.".format(key))
KeyError: 'Worksheet sheet range names does not exist'
So I thought it had something to do with not importing the entire openpyxl module. I proceeded to do that and run the whole process but it resulted in the same error.
Can someone please let me know what I am doing wrong/how to solve this?
Additional information:
I had successfully written to an empty file before, and then read the values. This gave me the right values for everything EXCEPT what I had written in manually via Excel- the cells that had manual input returned None or Nonetype. The issue seems to be with cells with manual input.
I did hit save on the file before accessing it don't worry
This was in the same directory so I know that it wasn't a matter of location.
The following command does not make sense:
sheet_ranges = wb['range names']
Normally you open a workbook and then access one of the worksheets, the following gives you some examples on how this can be done:
import openpyxl
wb = openpyxl.Workbook()
wb = openpyxl.load_workbook(filename = 'input.xlsx')
# To display all of the available worksheet names
sheets = wb.sheetnames
print sheets
# To work with the first sheet (by name)
ws = wb[sheets[0]]
print ws['A1'].value
# To work with the active sheet
ws = wb.active
print ws['A1'].value
# To work with the active sheet (alternative method)
ws = wb.get_active_sheet()
print ws['A1'].value
If you want to display any named range in the workbook, you can do the following:
print wb.get_named_ranges()
I'm not exactly sure what it is you need to do, but to read Excel spreadsheets into python, I usually use xlrd (which to me was easier to get use to). See example:
import xlrd
workbook = xlrd.open_workbook(in_fname)
worksheet = workbook.sheet_by_index(0)
To write to Excel spreadsheets, I use xlsxwriter:
import xlsxwriter
workbook = xlsxwriter.Workbook(out_fname)
worksheet = workbook.add_worksheet('spreadsheet_name')
Hope this helps.
I have a moderately large xlsx file (around 14 MB) and OpenOffice hangs trying to open it. I was trying to use openpyxl to read the content, following this tutorial. The code snippet is as follows:
from openpyxl import load_workbook
wb = load_workbook(filename = 'large_file.xlsx', use_iterators = True)
ws = wb.get_sheet_by_name(name = 'big_data')
The problem is, I don't know the sheet name, and Sheet1/Sheet2.. etc. didn't work (returned NoneType object). I could not find a documentation telling me How to get the sheet names for an xlsx files using openpyxl. Can anyone help me?
Use the sheetnames property:
sheetnames
Returns the list of the names of worksheets in this workbook.
Names are returned in the worksheets order.
Type: list of strings
print (wb.sheetnames)
You can also get worksheet objects from wb.worksheets:
ws = wb.worksheets[0]
As a complement to the other answers, for a particular worksheet, you can also use cf documentation in the constructor parameters:
ws.title
python 3.x
for get sheet name you must use attribute
g_sheet=wb.sheetnames
return by list
for i in g_sheet:
print(i)
**shoose any name **
ws=wb[g_sheet[0]]
or ws=wb[any name]
suppose name sheet is paster
ws=wb["paster"]
As mentioned the earlier answer
you can get the list of sheet names
by using the ws.sheetnames
But if you know the sheet names you can get that worksheet object by
ws.get_sheet_by_name("YOUR_SHEET_NAME")
Another way of doing this is as mentioned in earlier answer
ws['YOUR_SHEET_NAME']
for worksheet in workbook:
print(worksheet.name)