Python Xlrd and Xlwt - python

I have the following content in an old Excel sheet:
I need to generate a new Excel sheet with the following values:
In the input Excel file's 3rd column I have a range 10010-10040 and an increment value of 10. This needs to be expanded in my new Excel file.
If I give a comma (,) in between values, it should be treated as separate values and expanded. (Like in row2, column 3)
I have no idea how to do this and I am new to Python.

Try the following. This uses the xlrd and xlwt libraries to read and write xls spreadsheets:
import xlrd
import xlwt
wb_in = xlrd.open_workbook(r'input.xls')
sheet_name = wb_in.sheet_names()[0]
ws_in = wb_in.sheet_by_name(sheet_name)
wb_out = xlwt.Workbook()
ws_out = wb_out.add_sheet(sheet_name) # Use the same sheet name
row_out = 0
for row_in in range(ws_in.nrows):
row = ws_in.row_values(row_in)
if isinstance(row[2], float):
req_spec = str(int(row[2]))
else:
req_spec = row[2]
req_range = req_spec.split('-')
req_enum = req_spec.split(',')
if len(req_range) > 1: # e.g. 10010-10040-10
for value in range(int(str(req_range[0])), int(str(req_range[1])) + 1, int(str(req_range[2]))):
ws_out.write(row_out, 0, row[0])
ws_out.write(row_out, 1, row[1])
ws_out.write(row_out, 2, str(value))
row_out += 1
elif len(req_enum) > 1: # e.g. 1010,1020
for value in req_enum:
ws_out.write(row_out, 0, row[0])
ws_out.write(row_out, 1, row[1])
ws_out.write(row_out, 2, value)
row_out += 1
else: # e.g. 10100
ws_out.write(row_out, 0, row[0])
ws_out.write(row_out, 1, row[1])
ws_out.write(row_out, 2, req_spec)
row_out += 1
wb_out.save('output.xls')
Unfortunately, if you not familiar with Python there is quite a lot to take in.
The script works by creating an input workbook and an output workbook. For each row in the input, it assumes you will always have 3 columns and that the third one contains one of your three types of specifies. It decides which is in use based on whether or not there is a - or a , present. It then writes out rows to the output based on this range.
Note, when reading the file in, xlrd attempts to guess the format of the cell. For most of your entries, it guesses a string format, but sometimes it wrongly guesses a floating point number. The script tests for this and converts it to a string for consistency. Also xlrd uses unicode u"xxx" format to store the strings. These need to be converted to numbers to be able to calculate the required ranges.

Related

Applying xlsxwriter formula to entire column

I have tried this XLSXWriter Apply Formula Across Column With Dynamic Cell Reference and looking at the documentation, but I can't seem to figure out how to apply this to my code.
I have many formulas for new columns:
with xlsxwriter.Workbook('workbook.xlsx') as workbook:
worksheet = workbook.add_worksheet() #Creates excel doc and sheet
for row_num, data in enumerate(user_input):
worksheet.write_row(row_num, 0, data)
worksheet.write_formula('X3', '=W3/G3')
worksheet.write_formula('Y3', '=(X3-K3)/K3')
worksheet.write_formula('Z3', '=(X3-R3)/R3')
worksheet.write_formula('AA3', '=X3/N3')
worksheet.write_formula('AB3', '=(P3/Q3-1)')
worksheet.write_formula('AC3', '=O3/N3')
worksheet.write_formula('AD3', '=(R3-K3)/K3')
worksheet.write_formula('AE3', '=(X3/P3-M3/1000)/(X3/P3)')
worksheet.write_formula('AF3', '=(AA3-0.1)/0.1')
worksheet.write_formula('AG3', '=M3*N3/1000')
worksheet.write_formula('AH3', '=(R3-AG3)/AG3')
Currently, they are all using row 3 to make the calculations and adding the new columns into row 3. I want to write a loop that applies every single one of these formulas all the way down their columns beginning at row 2. There is not a specific amount of rows that will be added each time this is run, however if there has to be a range in order to create the loop I would do about 100 rows. Thanks in advance for any help.
Here is one way to do it using Python fstrings:
import xlsxwriter
# Sample data.
user_input = [range(23)] * 5
with xlsxwriter.Workbook('workbook.xlsx') as workbook:
worksheet = workbook.add_worksheet()
for row_num, data in enumerate(user_input, 3):
worksheet.write_row(row_num - 1, 0, data)
worksheet.write_formula(row_num - 1, 23, f'=W{row_num}/G{row_num}')
worksheet.write_formula(row_num - 1, 24, f'=(X{row_num}-K{row_num})/K{row_num}')
worksheet.write_formula(row_num - 1, 25, f'=(X{row_num}-R{row_num})/R{row_num}')
worksheet.write_formula(row_num - 1, 26, f'=X{row_num}/N{row_num}')
# ...
Output:

How to paste values only in Excel using Python and openpyxl

I have an Excel worksheet.
In column J i have some some source data which i used to make calculations in column K.
Column K has the values I need, but when i click on a cell the formula shows up.
I only want the values from column K, not the formula.
I read somewhere that i need to set data only=True, which I have done.
I then pasted data from Column K to Column L(with the intention of later deleting Columns J and K).
I thought that Column L will have only the values from K but if i click on a cell, the formula still shows up.
How do I simply paste values only from one column to another?
import openpyxl
wb = openpyxl.load_workbook('edited4.xlsx', data_only=True)
sheet = wb['Sheet1']
last_row = 100
for i in range(2, last_row):
cell = "K" + str(i)
a_cell = "J" + str(i)
sheet[cell] = '=IF(' + a_cell + '="R","Yes","No")'
rangeselected = []
for i in range (1, 100,1):
rangeselected.append(sheet.cell(row = i, column = 11).value)
for i in range (1, 1000,1):
sheet.cell(row=i, column=12).value = rangeselected[i-1]
wb.save('edited4.xlsx')
It's been a while since I've used openpyxl. But:
Openpyxl doesn't run an Excel formula. It reads either the formula string or the results of the last calculation run by Excel*. This means that if a calculation is created outside of Excel, and the file has never been open by Excel, then only the formula will be available. Unless you need to display (for historical purposes, etc.) what the formula is, you should do the calculation in Python - which will be faster and more efficient anyway.
* When I say Excel, I also include any Excel-like spreadsheet that will cache the results of the last run.
Try this (adjust column numbers as desired):
import openpyxl
wb = openpyxl.load_workbook('edited4.xlsx', data_only=True)
sheet = wb['Sheet1']
last_row = 100
data_column = 11
test_column = 12
result_column = 13
for i in range(2, last_row):
if sheet.cell(row=i, column=test_column).value == "R":
sheet.cell(row=i, column=result_column).value = "Yes"
else:
sheet.cell(row=i, column=result_column).value = "No"
wb.save('edited4.xlsx')
If you have a well-formed data sheet, you could probably shorten this by another step or two by using enumerate() and Worksheet.iter_rows() but I'll leave that to your imagination.

Using Python Count Number Of Populated Cells Populated In First Column

I am new to python and trying to learn - I have a spreadsheet that I need to open and count the populated cells (in first column) and print the count. Here is my code - I keep getting a traceback. Can you please assist?
book = xlrd.open_workbook('C:\\Users\\am\\Book1.xlsx')
sheet = book.sheet_by_name('Sheet1')
count = 0
for row in sheet.col(1):
count = count + 1
print (count)
You can use col_slice function for this.
import xlrd
book = xlrd.open_workbook('C:\\Users\\am\\Book1.xlsx')
sheet = book.sheet_by_name('Sheet1')
cells = sheet.col_slice(start_rowx=0,
end_rowx=sheet.nrows,
colx=0)
count = len(cells)
By the way, the first column's index is 0, as usual in Python.

Python- how do I write user input to an Excel file?

I'm new to Python so I hope this sounds right.
How could I use Python to write to an Excel file from user input?
I want my script to ask users "Name:" "Job Title:" "Building Number:" "Date:" etc. and from that raw input, fill in the corresponding columns one after the other in an Excel spreadsheet. I don't want future use of the script to overwrite previous data in the sheet either. I'd like each time to create a new line in the spreadsheet and then fill in the correct entries in each row. I hope that makes sense. Thank you so much in advance for your help.
You could use openpyxl to write to the workbook. Here's some basic usage, and should help avoid overwriting:
import openpyxl
wb = openpyxl.load_workbook('C:/test.xlsx')
ws = wb.active
i = 0
cell_val = ''
# Finds which row is blank first
while cell_val != '':
cell_val = ws['A' + i].value
i += 1
# Modify Sheet, Starting With Row i
wb.save('C:/test.xlsx')
Hope This Helps.
Edited, getting input and time:
For getting information from the user, use
x = input('Prompt: ')
However, if you want the actual current, I suggest using the time module:
>>> from time import strftime
>>> date = strftime('%m-%d-%y')
>>> time = strftime('%I:%M%p')
>>> print(date)
08-28-15
>>> print(time)
01:57AM
I will also add that XlsxWriter is also an excellent library for writing to Excel, however, unlike OpenPyXl, it is only a writer and does not read Excel files.
An example found from their documentation is as follows:
import xlsxwriter
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Expenses01.xlsx')
worksheet = workbook.add_worksheet()
# Some data we want to write to the worksheet.
expenses = (
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
)
# 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 item, cost in (expenses):
worksheet.write(row, col, item)
worksheet.write(row, col + 1, cost)
row += 1
# Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)')
workbook.close()
You may want to use the pandas module. It makes reading, writing, and manipulating Excel files very easy:
http://pandas.pydata.org/
Pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

python how to port from xlsxwriter to xlwt

I want to recreate an xlsxwriter program in xlwt.
I have issues writing a row. Can someone help me with the xlwt module? I found alot of code with xlwt using enumerate, but I am not too familiar with xlwt. The problem I have is, xlwt is writing the whole list as a string in the first cell, so I end up with one column full of data. The xlsxwriter writes each item in the list in its separate cell, which is what I want to do with xlwt. If someone can guide me in right direction, it will be greatly appreciated. thanks
this is my code:
def xlsxwriter_res(result):
workbook = xlsxwriter.Workbook('filename.xlsx')
for key,value in result.items():
worksheet = workbook.add_worksheet(key)
row, col = 0, 0
for line in value:
worksheet.write_row(row, col, line) ### Writes each item in list in separate cell
row += 1
workbook.close()
def xlwt_res(result):
workbook = xlwt.Workbook(encoding="utf-8")
for key,value in result.items():
worksheet = workbook.add_sheet(key)
row, col = 0, 0
for line in value:
worksheet.write(row, col, line) ### Writes the whole list as string in one cell
row += 1
workbook.save('filename.xls')
Try that:
import xlwt
def xlwt_res(result):
workbook = xlwt.Workbook(encoding="utf-8")
for key, value in result.items():
worksheet = workbook.add_sheet(key)
row = 0 # we assign 'col' later instead
for line in value:
# we're going to iterate over the line object
# and write directly to a cell, incrementing the column id
for col, cell in enumerate(line):
worksheet.write(row, col, cell) # writes the list contents just like xlsxwriter.write_row!
row += 1
workbook.save('filename.xls')
xlwt_res({'one': ["just one element"], 'two': ["that's a list", "did you know it"], 'three': ["let's", "have", "3"]})
So both xlwt and xlsxwriter yield the same results:

Categories

Resources