Pre-calculate Excel formulas when exporting data with python?

2024/11/15 17:21:36

The code is pulling and then putting the excel formulas and not the calculated data of the formula.

xxtab.write(8, 3, "=H9+I9")

When this is read in and stored in that separate file, it is stored as "=H9+I9" and not the actual value of what H9 + I9 is. Then when we are importing it back it still is not calculating what H9 + I9 actually is, but instead it is just re-pasting "=H9+I9" into excel.

The intended goal is to have the math done either during export, after it is exported but before it is imported to another file, or during import.

The code should not be just placing another math function/formula into the next excel sheet. It should be putting an answer to whatever was being read.

import xlsxwriter, string, csv, time, datetime, subprocess, os
import urllib, urllib2, requests, openpyxl, sys
import gspread# grab files
workbook = '/path/to/the/WorkBook.xlsx'# Setup the spreadsheet
print "Creating workbook"
book = xlsxwriter.Workbook(workbook, {'strings_to_numbers': True})# set matrix
row = 0  # row number
col = 0  # Column letter A=0 B=1 C=2 D=3...Z=26 ect.
# var.write(row, col, value, format)# Make the sheet
xxtab = book.add_worksheet('DB Tables')#Example of some .write commands which write to xxtab. The variables are taken directly from excel and are being read in by xlsl reader. This is stored in another file and is read in by the for loop.
xxtab.write(8, 1, "T_ACCESSRULES")
xxtab.write(8, 3, "=H9+I9")
xxtab.write(8, 7, "=data_dbmetrics!B56")# The for loop, reads in a .txt file and does multiple .write commands
with open('/path/to/my/file/interns_xxtables.txt', 'rb') as f:reader = csv.reader(f, delimiter='\t')for c, col in enumerate(reader):if(len(col) > 3):if (str(col[3]) == "bold"):xxtab.write(int(col[0]), int(col[1]), col[2], bold)elif (col[3] == "metric"):xxtab.write(int(col[0]), int(col[1]), col[2], metric)elif (col[3] == "title"):xxtab.write(int(col[0]), int(col[1]), col[2], title)else:xxtab.write(int(col[0]), int(col[1]), col[2])
f.close()

Sorry, I am new to Stack Overflow. I am not sure how else to describe this question. I don't know how to get what I want, I just know what I want is not being achieved with what I have written.

Answer

The intended goal is to have the math done either during export, after it is exported but before it is imported to another file, or during import.

XlsxWriter, the module shown writing the Excel file, doesn't evaluate the result of the formulas that it writes. If you calculate the result in your code you can add it when writing the formula:

xxtab.write(8, 3, "=H9+I9", 42)

This is explained in more detail in the Working with Formulas section of the XlsxWriter documentation.

None of the Python Excel file writing modules evaluate formulas. The only modules that could do that are ones that automate Excel such as XlWings.

https://en.xdnf.cn/q/119030.html

Related Q&A

Validating Tkinter Entry Box

I am trying to validate my entry box, that only accepts floats, digits, and operators (+-, %). But my program only accepts numbers not symbols. I think it is a problem with my conditions or Python Rege…

Imaplib with GMail offsets uids

Im querying my gmail inbox using pythons ImapLib with a range parameter, but my returned uids are offset from what I request. My request is as follows:M = imaplib.IMAP4_SSL(imap.gmail.com) M.login(USER…

Accessing a folder containing .wav files [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

What is the right Python idiom for sorting by a single criterion (field or key)? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…

Incorrect checking of fields in list using a for loop

I have the following code that seeks to read the file contents into a list (this bit works) and then display a message of acceptance, IF the bank details (user and corresponding number) matches. e.g. i…

Why myVar = strings.Fields(scanner.Text()) take much more time than comparable operation in python?

Consider the following code in golangnow := time.Now() sec1 := now.Unix()file, err := os.Open(file_name) if err != nil {log.Fatal(err) } defer file.Close()scanner := bufio.NewScanner(file)var parsedLin…

When reading an excel file in Python can we know which column/field is filtered

I want to capture the field or column name that is filtered in the excel file when reading through python. I saw that we can also capture only the filtered rows by using openpyxl and using hidden == Fa…

Error:__init__() missing 1 required positional argument: rec

I am new to python. I am trying to do microphone file that ought to detect, listen, record and write the .wav files. However, it is giving me an error while I am trying to run the file. It is saying:Ty…

Maya: Connect two Joint chains with Parent Constraint

So here is a snipit of an IK spine builder Ive been working on. Ive figure out how to make lists to duplicate the bound into an IK chain, what Ive got stuck on however is I want my list and for loop to…

What is the equivalent for onkeydown and onkeyup (Javascript events) in python?

There are events called onkeydown and onkeyup in Javascript. Can anyone please suggest the python equivalent of it?