xlsxwriter refer to sheets inside worksheet.write_formula() - python

I am trying to find a solution how to substitute the following:
worksheet = writer.sheets['Overview']
worksheet.write_formula('C4', '=MIN('Sheet_147_mB'!C2:C325)')
with something like:
for s in sheet_names:
worksheet.write_formula(row, col, '=MIN(s +'!C2:C325')')
row+=1
to iterate through all the existing sheets in the current xlsx book and write the function to the current sheet having an overview.
After spending some hours I was not able to find any solution therefore it would be hihgly appriciated if someone could point me to any direction. Thank you!

You don't give the error message, but it looks like the problem is with your quoting - you can't nest single quotes like this: '=MIN(s +'!C2:C325')'), and your quotes aren't in the right places. After fixing those problems, your code looks like this:
for s in sheet_names:
worksheet.write_formula(row, col, "=MIN('" + s +"'!C2:C325)")
row+=1
The single quotes are now nested in double quotes (they could also have been escaped, but that's ugly), and the sheet name is enclosed in single quotes, which protects special characters (e.g. spaces).

Related

printSchema having all columns in the first one

I have loaded a text file using the load csv function but when I try to print the schema it shows just one field from the root including every row in that one. like this:
root
|-- Prscrbr_Geo_Lvl Prscrbr_Geo_Cd Prscrbr_Geo_Desc Brnd_Name
Any idea how to fix this?
Adding my comment as an answer since it seems to have solved the problem.
From the output, it looks like the CSV file is actually using tab characters as the separator between columns instead of commas. To get Spark to use tabs as the separator, you can use spark.read.format("csv").option("sep", "\t").load("/path/to/file")

Openpyxl lowercase VLOOKUP

I am working on a script to help my boss modify .xlsx files that he gets. I'm trying to insert a VLOOKUP into every cell in a column, but I am running into an issue where some of the letters inside the parenthesis are being changed to lowercase.
This is the code I am using:
import openpyxl
wb = openpyxl.load_workbook('wb.xlsx')
ws = wb['Sheet1']
for row in ws['J1:J847']:
for cell in row:
cell.value = '=VLOOKUP(A{0}, Collection.A:G,7,0)'.format(cell.row)
wb.save('test.xlsx')
The output in the spreadsheet is:
=VLOOKUP(A1, collection.a:g,6,0)
I need it to look like:
=VLOOKUP(A1, Collection.a:g,6,0)
or even better:
=VLOOKUP(A1, Collection.A:G,6,0)
I have checked to ensure that the string is being formatted correctly. What I find most confusing is that not all of the uppercase characters are being switched. What am I doing wrong and what is happening under the covers to cause something like this?
I had a similar problem and it was fixed by replacing the period by an exclamation point.
=VLOOKUP(A{0}, Collection!A:G,7,0)

Adding linebreaks within cells in CSV - Python 3

This is essentially the same question asked here: How can you parse excel CSV data that contains linebreaks in the data?
But I'm using Python 3 to write my CSV file. Does anyone know if there's a way to add line breaks to cell values from Python?
Here's an example of what the CSV should look like:
"order_number1", "item1\nitem2"
"order_number2", "item1"
"order_number3", "item1\nitem2\nitem3"
I've tried appending HTML line breaks between each item but the system to where I upload the data doesn't seem to recognize HTML.
Any and all help is appreciated.
Thanks!
Figured it out after playing around and I feel so stupid.
for key in dictionary:
outfile.writerow({
"Order ID": key,
"Item": "\n".join(dictionary[key])
})
Here's an example of what the CSV should look like:
"order_number1", "item1\nitem2"
"order_number2", "item1"
"order_number3", "item1\nitem2\nitem3"
The proper way to use newlines in fields is like this:
"order_number1","item1
item2"
"order_number2","item1"
"order_number3","item1
item2
item3"
The \n you show are just part of the string. Some software may convert it to a newline, other software may not.
Also try to avoid spaces around the separators.

Python csv.writerow([]) across two lines?

My code is:
c = csv.writer(open("addresses.csv", "wb"), quoting=csv.QUOUTE_ALL)
temp = open("temp_address.txt") //this contains a string, like this "1234567890ABCDEF"
c.writerow([temp.read(), "customers withdraw"])
The .csv file ends out like this
"1234567890ABCDEF
","Customers widthraw"
Why does this happen? I would appreciate some help to fixing this
It looks like the file has a newline at the end. You might want to .strip() the results.
e.g.:
c.writerow([temp.read().strip(), "customers withdraw"])

xlwt use Hyperlink with variable

I would like to use xlwt to generate hyperlink in particular cell. I tried to put following in formula, it's fine:
Hyperlink("http://www.google.com";"Link")
But when I define X='"http://www.google.com"' (Note the single quote outside of double quote)
and then:
Hyperlink(X;"Link")
It won't work.
Basically, I want to put a variable X, which could be different when the program runs, into Hyperlink(). Any idea to fix this problem would be appreciated!
Use this construct
click='"http://www.google.com"'
wsheet.write(j,8,xlwt.Formula('HYPERLINK(%s;"Link")' % click))
or, easier to read and maintain:
click='http://www.google.com'
wsheet.write(j,8,xlwt.Formula('HYPERLINK("%s";"Link")' % click))
For details of the % operator for string formatting, see
http://docs.python.org/2/library/stdtypes.html#string-formatting

Categories