spettitt Posted October 24, 2023 Share Posted October 24, 2023 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): Is there a known working way to get a csv file to write from Python executed in Vectorworks? Quote Link to comment
twk Posted October 24, 2023 Share Posted October 24, 2023 (edited) 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 October 24, 2023 by twk 1 Quote Link to comment
JBenghiat Posted October 24, 2023 Share Posted October 24, 2023 Unless this is part of a larger process, you can also export csv from the worksheet’s file menu Quote Link to comment
spettitt Posted October 25, 2023 Author Share Posted October 25, 2023 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...). 1 Quote Link to comment
spettitt Posted October 25, 2023 Author Share Posted October 25, 2023 P.S. fixed it! ChatGPT said the \ was triggering an escape sequence in python, and prefixing the string with 'r' to format it as a raw string disables this behaviour. Leaving here for future explorers with the same issue. 1 Quote Link to comment
DomC Posted October 25, 2023 Share Posted October 25, 2023 "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') 2 Quote Link to comment
Recommended Posts
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.