Jump to content

CSV Export using Python


Recommended Posts

I'm about to start on a task for taking given named worksheet, recalculating it and then exporting the contents as a CSV. I've started at the export bit and I'll look at getting the data to go in it later.

 

I started to use vs.PutFile, which allowed me to output a blank file, but Write, WriteLn etc weren't known by the interpreter. So I presume this is because Python should really be using more native ways for that kind of thing.

 

So, executing in PyCharm, I can export a CSV file using the below fine, but get 'Errno13'  'Permission Denied' if executing in VWX.

 

import csv

data = ["Circuit", "Cable", "Signal"]
filename = "output.csv"

with open(filename, 'w', newline='') as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(data)

 

If I specify a file path at the definition of the filename variable ("C:\.....output.csv"), I get this (I can't copy and paste from the script error log screen, hence the screenshot):

 

image.thumb.png.a1f9580587b70d7e16f5548dd1241589.png

 

Is there a known working way to get a csv file to write from Python executed in Vectorworks?

Link to comment

the path has to be absolute when executing inside vectorworks. Not:

filename = "output.csv"

leaving it as "output.csv" is telling Vectorworks' python interpreter to save it the installation directory of vectorworks.

Try specifying a full path to another location eg.

filename = "C:\Temp\output.csv"

 

 

Also look at DataFrames for python using the pandas module. It has better functionalites for Data Wrangling.

You'll need to write your own module for converting a worksheet table to a pandas dataframe, then once its in dataframe format, you can do many things with that dataframe. Export to csv, xls, json, etc

 

Edited by twk
  • Like 1
Link to comment
13 minutes ago, twk said:

the path has to be absolute when executing inside vectorworks. Not:

filename = "output.csv"

leaving it as "output.csv" is telling Vectorworks' python interpreter to save it the installation directory of vectorworks.

Try specifying a full path to another location eg.

filename = "C:\Temp\output.csv"

 

I did that, but I got the SyntaxError (unicode error) in my little screenshot. Uneducated guess that it's referring to the \ as an invalid character?

 

13 minutes ago, twk said:

Also look at DataFrames for python using the pandas module. It has better functionalites for Data Wrangling.

You'll need to write your own module for converting a worksheet table to a pandas dataframe, then once its in dataframe format, you can do many things with that dataframe. Export to csv, xls, json, etc

 

 

Great, thanks, will do. I'm aware of pandas but haven't used it yet, so I'll dive in to it.

 

10 minutes ago, JBenghiat said:

Unless this is part of a larger process, you can also export csv from the worksheet’s file menu

 

Thanks, I'm trying to make it slick and fast for some our of users to export a batch of specific worksheets in our template to a target folder for import to Access (bleughhh) for reporting. The slicker and faster I make it, the more likely they are to use it...

 

Really hoping that Paginated Worksheets come soon, so that I don't have to push out to an external app that understands paginated reporting. I can make some really nice reports, checklists etc in Access, but having to transfer all of the data externally is tedious (tried to get the ODBC thing functioning at one point, sod that...).

 

  • Like 1
Link to comment

"C:\\Temp\\Output.csv" would work also
I would strongly recommend,  always create path strings with the path module. So it is as proof as it can be and this works also cross-platform:
 

import os
path = os.path.join('C:', 'Temp', 'Output.csv') 

# or even more propper
path = os.path.join('C', os.sep, 'Temp', 'Output.csv') 


 

  • Like 2
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...