LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2017, 07:12 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
Question about generating spreadsheets


I use a python script found on stackoverflow to read in multiple csv files and create an excel workbook with separate sheets corresponding to each csv file.

Here is the code.
Code:
#!/usr/bin/python

import csv, xlwt, os

data_folder_path = '/home/path/to/my/cvs/files'

def input_from_user(prompt):
    return raw_input(prompt).strip()

def make_an_excel_file_from_all_the_txtfiles_in_the_following_directory(directory):
    wb = xlwt.Workbook()
    for filename in os.listdir(data_folder_path):
        #if filename.endswith(".csv") or filename.endswith(".txt"):
        if filename.endswith(".csv"):
            ws = wb.add_sheet(os.path.splitext(filename)[0])
            with open('{}/{}'.format(data_folder_path,filename),'rb') as csvfile:
                reader = csv.reader(csvfile, delimiter=',')
                for rowx, row in enumerate(reader):
                    for colx, value in enumerate(row):
                        ws.write(rowx, colx, value)
    return wb

if __name__ == '__main__':
    path_to_data = input_from_user("Where is the data stored?: ")
    xls = make_an_excel_file_from_all_the_txtfiles_in_the_following_directory(path_to_data)
    xls_name = input_from_user('What do you want to name the excel file?: ')
    xls.save('{}/{}{}'.format(data_folder_path,xls_name,'.xls'))
    print "Your file has been saved in the data folder."
This does work however, it formats the cells in away that breaks cell references. For example, suppose I have a sheet called 'Teams' and cell B2 of Teams has the text value "Tigers". Further suppose that in one of my csv files I have '=Teams.B2' as one of the comma separated values. The spreadsheet gets created and the sheet in question has the value '=Teams.B2' in place but "=Teams.B2" is displayed instead of "Tigers".

I've done the normal checks on the format of the cell and that is not the problem. I can set the cell to any number of types and it continues to display "=Teams.B2" (without the double quotes btw).

If I retype part of the cell contents it de-references properly and displays "Tigers". For example, I can go into the cell, backspace over the 2, re-type 2, then hit enter and I see "Tigers" displayed.

So far I have only tried this with libreoffice calc so this may not be an issue with other spreadsheets.

If anyone else has created excel workbooks in a similar manner with cell references and got it to work I would love to know your secret. Am also open to other comments or suggestions.

Cheers,
 
Old 07-17-2017, 07:29 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
It might be a long shot, but after the sheet has been populated, have you tried Shift+Ctrl+F9 to force a recalculation of all formulas in the document?
 
Old 07-17-2017, 08:02 PM   #3
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by hydrurga View Post
It might be a long shot, but after the sheet has been populated, have you tried Shift+Ctrl+F9 to force a recalculation of all formulas in the document?
Doesn't work. Thanks anyway. I'd been looking for the command actually.
 
Old 07-17-2017, 09:11 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,138

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
Does the source csv(s) have the dereference as a string (double-quoted) ?. Might affect how it is being imported.
 
Old 07-17-2017, 09:53 PM   #5
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by syg00 View Post
Does the source csv(s) have the dereference as a string (double-quoted) ?. Might affect how it is being imported.
Double quotes does not work. Also tried single quotes and they get displayed in the cell.
 
Old 07-18-2017, 02:44 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,732

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
In Excel:

Quote:
Switch between displaying formulas and their results

Press CTRL + ` (grave accent).
[Also called a backtick]
 
Old 07-18-2017, 05:12 PM   #7
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by scasey View Post
In Excel:

[Also called a backtick]
This does not work. It will work on a cell that I have manually fixed already but not on cells left as are. They continue to display only the formulas.
 
Old 07-18-2017, 11:19 PM   #8
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Solution

Turns out I have to use strict excel syntax for referencing cells from different sheets. In my csv files I replaced all occurrences SHEETNAME.CELLNAME with SHEETNAME!CELLNAME and the script got past that error. However if a worksheet (ws) for SHEETNAME is not already created in the workbook (wb) then another error will be created.

This is not a major issue for me because there was only one reference worksheet so I created a separate def in the python script for that one sheet. With that done the other def runs with no issues and the de-referenced value is now shown in the cells instead of the formula in libreoffice.

Hope that is clear.

Cheers,
 
Old 07-19-2017, 02:48 AM   #9
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Is it possible to use
Quote:
ws.write(rowx, colx, value&<CRLF>)
instead of just
Quote:
ws.write(rowx, colx, value)
OK
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Generating Pseudo-Random Numbers - Efficiency Question killingthemonkey Programming 2 09-19-2016 10:13 PM
[SOLVED] A question about generating base64 strings... trist007 Linux - Newbie 2 03-16-2011 01:21 PM
[SOLVED] A innocent question about generating CSV file with php koshihaku Linux - Server 2 08-24-2010 08:47 PM
compare spreadsheets binary_dreamer Linux - Software 6 02-03-2009 04:44 AM
Statistics and Programming Question, generating a random number firefly2442 Programming 2 01-06-2006 04:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration