Applying xlsxwriter formula to entire column - python

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:

Related

Quotation issues while writing formulas in xlsxwriter

I need to write SUMIFS formulas in Excel using xlsxwriter. I think this may be more of a quotations in strings question but I'm not sure.
Example
=SUMIFS('datasheet'!N:N,'datasheet'!D:D,'Sch B'!B:B,'datasheet'!J:J,"G")
I took the documentation code and added the example SUMIFS formula on line23. It gets wonky at "G".
Documentation Code
import xlsxwriter
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('output.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)
worksheet.write_formula(row, col + 2, "=SUMIFS('datasheet'!N:N,'datasheet'!D:D,'Sch B '!B:B,'datasheet'!J:J,"G")")
row += 1
workbook.close()
Thank you!
The solution is a string based solution. Adding \" in front of the quotation to protect it.
=SUMIFS('datasheet'!N:N,'datasheet'!D:D,'Sch B'!B:B,'datasheet'!J:J,\"G\")

how can I iterate specific rows of excel worksheet if I have row numbers using openpyxl?

I am new to programming, currently learning python and openpyxl for excel file manipulations.
I am writing a script to help in updating repairs database which picks specific records in an excel sheet.
I have written a script where I can get the row numbers of the excel data I need to update but the challenge now is about how to create a list within a list of a row (record) through iteration.
For example I have found that I need data from rows 22, 23, 34 & 35. Is there a way of getting the data of these rows without having to change min_row and max_row number for every instance of row?
the portion of the code that I need rewritten is:
# copy the record on the rows of the row numbers found above.
rows_records = []
row_record = []
for row_cells in ws2.iter_rows(min_row=23, max_row=23, min_col=1, max_col = 6):
for cell in row_cells:
row_record.append(cell.value)
rows_records.append(row_record)
print(row_record)
print(rows_records)
enter image description here
Technically the same as accepted answer
sheet : Sheet = book.active
indices : set[int] = {10, 20, }
rows : Iterable[int] = (row for i, row in enumerate(sheet.rows, 1) if i in indices)
for row in rows:
... do whatever
Since openpyxl return a generator you have to convert it to a list to access an index in particular
import openpyxl
workbook = openpyxl.load_workbook('numbers.xlsx')
worksheet = workbook .active
rows = list(worksheet.iter_rows(max_col=6, values_only=True))
values = []
row_indices = [21, 22, 23, 34, 35]
for row_index in row_indices:
values.append(rows[row_index])
import openpyxl
book = openpyxl.load_workbook('numbers.xlsx')
sheet = book.active
rows = sheet.rows
values = []
your_row_indices = [21,22,23]
for row in rows[your_row_indices]:
for cell in row:
values.append(cell.value)

How to set print_area and repeat_rows using xlsxwriter, if I need custom sheet names?

How can I create a xlsx file for which I can specify all the following features using xlsxwriter?
A custom name for each sheet (add_worksheet)
A specific print area (print_area)
Repeatition of the first row of the sheet on every printed page (repeat_rows)
I tried to use (something similar to) the following code:
import xlsxwriter
import pandas as pd
from random import randint
df = pd.DataFrame({'A': [randint(2000, 2001) for x in range(150)],
'B': [randint(300, 400) for x in range(150)]})
workbook = xlsxwriter.Workbook('xlsxwriter-problem.xlsx')
main_format = workbook.add_format({'border': 1})
for name, group in df.groupby('A'):
worksheet = workbook.add_worksheet(str(name))
worksheet.write(0, 0, 'B')
for i in range(1, len(group.index)):
worksheet.write(i, 0, group.iloc[i,1])
worksheet.set_column(0, 0, None, main_format)
worksheet.print_area('A1:A95')
worksheet.repeat_rows(0)
workbook.close()
However, when I open the file in LibreOffice Calc, I notice the last two lines inside the loop seem to be ignored, and I get this unwanted behaviour.
After a while, I found out that these two lines produce the desired behaviour if I use the default sheet names, that is, if I set
worksheet = workbook.add_worksheet()
inside the loop.
Isn't possible to keep the both custom sheet name and the other features?

Python Xlrd and Xlwt

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.

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.

Categories

Resources