I am trying to write the output retrieved from a large Excel workbook in another spreadsheet using Python. However, I am not able to, it's giving me errors such as raise ValueError("column index (%r) not an int in range(256)" % arg)ValueError: column index (256) not an int in range(256), Exception: Unexpected data type .
I can understand these errors to some extent but not able to rectify my code. I have written a small script here. It will be great if some one can tell me and correct me where I am going wrong.
import xlrd
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
file_location = "path/lookup_V1.xlsx"
workbook=xlrd.open_workbook(file_location)
sheet1 = workbook.sheet_by_index(0)
print sheet1.name
sheet2 = workbook.sheet_by_index(1)
print sheet2.name
print workbook.nsheets
st1c=sheet1.ncols
st2r=sheet2.nrows
st1=st1c+1
st2=st2r+1
print("fill..")
for i in xrange(0,st1c):
s1=sheet1.col_values(i)
i+1
s1.sort()
print s1
for col in xrange(st1c):
for row in xrange(st2r):
print("filling sheet...")
col=col+1
row=row+1
ws.write(row,col)
print("here")
wb.save('testfile.xls')
Try this :
import xlrd
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
file_location = "test.xls"
workbook=xlrd.open_workbook(file_location)
sheet1 = workbook.sheet_by_index(0)
st1c=sheet1.ncols
st1r=sheet1.nrows
for col in xrange(st1c):
for row in xrange(st1r):
value = sheet1.col_values(col, row, row + 1)
# According to documentation : http://www.lexicon.net/sjmachin/xlrd.html#xlrd.Sheet.col_values-method
# col_values returns a slice of the values of the cells in the given column.
# That why we have to specify the index below, it is no elegant but it works
ws.write(row, col, value[0])
wb.save('testfile.xls')
This solution work for a single sheet... You may then convert it into a function and iterate over different sheets and workbook...
More elegant solution :
import xlrd
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
file_location = "test.xls"
workbook=xlrd.open_workbook(file_location)
sheet1 = workbook.sheet_by_index(0)
st1c=sheet1.ncols
st1r=sheet1.nrows
for col in xrange(st1c):
for row in xrange(st1r):
# More elegant
value = sheet1.cell_value(row, col)
ws.write(row, col, value)
wb.save('testfile.xls')
Related
I have tried succesfuly to populate the data in QTableWidget using Pandas.
Now i want to export to specific rows and columns to an existing excel so i will not lose stylesheet and other data from this excel. Please , help me out finding the solution to run it properly.
Goal is to export to excel to specific rows and columns, rows in range from 7 to 30 and columns from 1 to 13 using OpenPyxl to just modify values of an existing excel. I know "appends" means to add whole data table on the bottom of the excel and i don't know what function to use instead.
def kalkuacje_exportuj(self):
columnHeaders = []
# create column header list
for j in range(self.ui.tableWidget.model().columnCount()):
columnHeaders.append(self.ui.tableWidget.horizontalHeaderItem(j).text())
df = pd.DataFrame(columns=columnHeaders)
# create dataframe object recordset
for row in range(self.ui.tableWidget.rowCount()):
for col in range(self.ui.tableWidget.columnCount()):
df.at[row, columnHeaders[col]] = self.ui.tableWidget.item(row, col).text()
from openpyxl import Workbook
wb = Workbook()
wb = load_workbook ('OFERTA_SZABLON.xlsx')
# ws1 = wb.sheetnames()
ws1 = wb["DETALE wyceniane osobno"]
# for row in ws1.iter_rows(min_row=7,
# max_row=30,
# min_col=1,
# max_col=13):
for row in range(7, 30):
for col in range(1, 13):
for r in dataframe_to_rows(df, index=False, header=False):
ws1.append(r)
# for cell in row:
# print(cell)
wb.save('OFERTA_SZABLON.xlsx')
I solved the problem like this:
from openpyxl import Workbook
wb = Workbook()
wb = load_workbook ('OFERTA_SZABLON.xlsx')
# ws1 = wb.sheetnames()
ws1 = wb["DETALE wyceniane osobno"]
# for r in dataframe_to_rows(df, index=False, header=False):
# ws1.append(r)
offset_row = 5
offset_col = 0
row = 1
for row_data in dataframe_to_rows(df, index=False, header=False):
col = 1
for cell_data in row_data:
ws1.cell(row + offset_row, col + offset_col, cell_data)
col += 1
row += 1
wb.save('OFERTA_SZABLON.xlsx')
I cannot figure this out for the life of me.
the guy above me has an error with >>> load_workbook ('OFERTA_SZABLON.xlsx')
it makes no sense and Workbook.load_workbook('') isn't a thing anyways
dataframe_to_rows doesn't seem to exist either
I'd like to read the values from column B in every worksheet within my workbook.
After a fair amount of reading and playing around I can return the cell names of the cells I want the values from, but I can't figure out how to get the values.
from openpyxl import load_workbook
wb = load_workbook(r"C:/Users/username/Documents/test.xlsx")
for sheet in wb.worksheets:
for row in range(2,sheet.max_row+1):
for column in "B":
cell_name = "{}{}".format(column, row)
print (cell_name)
This is returning the cell names (i.e. B2, B3) that have values in column B in every worksheet.
According to the documentation https://openpyxl.readthedocs.io/en/stable/usage.html you can access cell values as:
sheet['B5'].value
Replace B5 with the cell(s) you need.
import xlrd
loc = ("foo.xlsx") # excel file name
wb = xlrd.open_workbook(loc)
# sheet = wb.sheet_by_index(0)
for sheet in wb.sheets():
for i in range(sheet.nrows):
print(sheet.cell_value(i, 1))
Edit: I edited my answer to read all sheets in excel file.
just play with the range
from openpyxl import load_workbook
wb = load_workbook('')
for sheet in wb:
for i in range(1,50):
if sheet['B'+str(i)].value:
print(sheet['B'+str(i)].value)
Better one,
from openpyxl import load_workbook
wb = load_workbook('')
for sheet in wb:
for row in sheet['B']:
print(row.value)
I need to create an excel file which contains the output of different functions, such as dates, the number of duplicated variables...
I managed to create an excel file that contains the output of the first function but when I tried to append it with the output of the 2nd funcion, it did not work. I don't know how to add the outputs of my different functions to the same excel file.
Example:
function1:
import xlsxwriter
import openpyxl
import pandas as pd
def NGTP():
VIN_FFF = 0
N_Alert =0
for x in flat_list:
if "NGTPSM HTTP request" in str(x):
VIN_FFF+=1
N_Alert =5
workbook = xlsxwriter.Workbook('Results.xlsx')
worksheet = workbook.add_worksheet("sheet")
parametres = (
['VIN_FFF ', VIN_FFF],
['Nombre d alertes ', N_Alert],
)
# Start from the first cell. Rows and
# columns are zero indexed.
row = 0
col = 0
# Iterate over the data and write it out row by row.
for name, parametres in (parametres):
worksheet.write(row, col, name)
worksheet.write(row, col + 1, parametres)
row += 1
workbook.close()
function2:
import xlsxwriter
from openpyxl import load_workbook
from openpyxl import Workbook
import pandas as pd
filename = "Results.xlsx"
def Date2Roulage():
file_name='OutputDLT.xlsx'
df = pd.read_excel(file_name ,header=0, index= False)
d= df.iloc[0,2]
#d='2015-01-08 22:44:09'
date=pd.to_datetime(d).date()
date=list(date)
print('Date de roulage: ', date)
workbook = xlsxwriter.Workbook('Results.xlsx')
worksheet = workbook.add_worksheet("sheet")
parametres = (
['Date de roulage ', date],
)
# Start from the first cell. Rows and
# columns are zero indexed.
row = 2
col = 0
# Iterate over the data and write it out row by row.
for name, parametres in (parametres):
worksheet.write(row, col, name)
worksheet.write(row, col + 1, parametres)
row += 1
for row_ in parametres:
worksheet.append(row_)
workbook.save(filename)
workbook.close()
It does not produce an error, but I guess it's not working because i'm trying to create the same file twice simultaneously.
this is my main code:
from Date_de_roulage_Fct import *
from Navco_Fct import *
Date2Roulage()
Navco()
I've updated the second function, according to the suggested solution, but I got an error:
TypeError: 'datetime.date' object is not iterable
I trie this solution date=list(date) and it did not work
The generated excel file must be vreated as shown is the image below:
You cannot append new rows to an exsisting file with xlsxwriter.
What you can do is use openpyxl (the which you have imported) that natively has this option:
from openpyxl import Workbook
from openpyxl import load_workbook
filename = "Results.xlsx"
new_row = ['a', 'b', 'c']
# Confirm file exists.
# If not, create it, add headers, then append new data
try:
wb = load_workbook(filename)
ws = wb.worksheets[0] # select first worksheet
except FileNotFoundError:
# excel header if the file does not exists
headers_row = ['Header 1', 'Header 2', 'Header 3']
wb = Workbook()
ws = wb.active
ws.append(headers_row)
ws.append(new_row)
wb.save(filename)
If you prefer to use xlsxwriter you could read all the previous rows, and rewrite them all with the new data.
For your input, you need to as follow:
for row_ in parametres:
ws.append(row_)
wb.save(filename)
Trying to find out how to print to a specific column/row similar to how
pd.to_excel(startcol = 1, startrow = 1) works. I have to do this in an open excel workbook, and found the library xlwings. I'm currently using openpyxl, how would I do this in xlwings? I read the documentation printing to specific cells like A1, but not by specifying columns/rows.
#Write to Excel
book = load_workbook('Test.xlsx')
writer = pd.ExcelWriter('Test.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
def addtoexcel(df, row):
i = 0
df[["val1", "val2"]] = df[["val1", "val2"]].apply(pd.to_numeric)
line = int(df.loc[i][1])
for i in range(1, line+1):
if line ==i:
df = df.T
df.to_excel(writer, "test", index = False, header = False, startcol = line+2, startrow = row)
How can I print in xlwings by specifying column/row like (1,1)?
You can easily print a pandas dataframe to excel using xlwings. The range object takes a row and a column number as arguments (or just a cell reference as a string). Consider the following code:
import xlwings as xw
import pandas as pd
row = 1
column = 2
path = 'your/path/file.xlsx'
df = pd.DataFrame({'A' : [5,5,5],
'B' : [6,6,6]})
wb = xw.Book(path)
sht = wb.sheets["Sheet1"]
sht.range(row, column).value = df
You can also add options to include index/header:
sht.range(row, column).options(index=False, header=False).value = df
I am currently have a workbook with 5 worksheets. there is data in columns a - e, while each worksheet may have data in the same columns, each worksheet has different different amount rows though. I am entering a formula in column f that will go from cell F4 to whatever the last row is in that worksheet.
I am able to loop through sheets and am able to create a formula that goes from F4 to the last row, however I am unable get the two to work together.
Code
import os
import os.path
import urllib
import xlrd
import xlwt
from xlutils.copy import copy
fname = "test.xls"
destination = 'C:\Users\Raj Saha\Google Drive\Python\Excel-Integration'
rb = xlrd.open_workbook(fname,formatting_info=True) #original workbook
r_sheet = rb.sheet_by_index(1) #origianl worksheet
style = xlwt.easyxf('font: bold 1')
wb = copy(rb) #virtual workbook
#sheet = wb.get_sheet(1)
shxrange = range(rb.nsheets)
sh = rb.sheet_by_name("Data 1")
#print "total rows: %d, rows less headers: %d" % (nrows, rows)
for sheet in shxrange:
nrows = sheet.nrows
rows = nrows - 4
for i in range(rows):
sheet.write(i+3, 5, xlwt.Formula("B%d-C%d" % (i+4, i+4)))
sheet.write(2,5,"CL1-CL2",style)
wb.save(fname)
I get the following error message:
File "C:/formulas_multi_sheets.py", line 31, in
nrows = sheet.nrows
AttributeError: 'int' object has no attribute 'nrows'
I assume the error in line 31 would apply to line 32. I am using python 2.7.
Here, shxrange gives you integers. What you need is sheet class object. For getting object of every sheet of your workbook,
for sheet_no in shxrange:
sheet=rb.sheet_by_index(sheet_no)
nrows = sheet.nrows
rows = nrows - 4
for i in range(rows):
sheet.write(i+3, 5, xlwt.Formula("B%d-C%d" % (i+4, i+4)))
sheet.write(2,5,"CL1-CL2",style)
Due to the limitations of xlrd and wlwt, i turned to win32com. Here is the following solution that i came up with. I am able to sort data in multiple worksheets and add formulas to multiple worksheets.
Code
import win32com.client as win32
import types
from win32com.client import constants as c
fname = ('C:/Users/test.xls')
xlApp = win32.gencache.EnsureDispatch('Excel.Application')
xlApp.Visible = True
xlDescending = 2
xlSortColumns = 1
sh_range = range(2,6)
wb = xlApp.Workbooks.Open(fname)
for sh in wb.Sheets:
rangeFrom = 'F4'
column = 'F'
lastRow = sh.UsedRange.Rows.Count
rangeTo = rangeFrom + ':' + column + str(lastRow)
print "%r" % rangeTo
xlApp.Worksheets(sh.Name).Activate()
ws = xlApp.ActiveSheet
ws.Range('f3').Value = "CL1-CL2"
ws.Range('f4').Formula = "=B4-C4"
ws.Range('f4:f4').Select()
xlApp.Selection.AutoFill(ws.Range(rangeTo),win32.constants.xlFillDefault)
for i in sh_range:
xlApp.Sheets(i).Range("A4:E50000").Sort(Key1=xlApp.Sheets(i).Range("A4"), Order1=xlDescending, Orientation=xlSortColumns)
wb.Save()
wb.Close()