I am trying to convert from stp to stl. The code is:
rom OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.StlAPI import StlAPI_Writer
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
input_file = 'stl_test.stp' # input STEP (AP203/AP214 file)
output_file = 'stl_test.stl' # output X3D file
step_reader = STEPControl_Reader()
step_reader.ReadFile( input_file )
step_reader.TransferRoot()
myshape = step_reader.Shape()
print("File readed")
# Export to STL
stl_writer = StlAPI_Writer()
stl_writer.SetASCIIMode(True)
stl_writer.Write(myshape, output_file)
print(stl_writer.Write(myshape, output_file))
print("Written")
THis is not saving anything in the working dir. And print(stl_writer.Write(myshape, output_file)) is giving false output.
Can anyone help?
I had the same problem, even though your way is like it is told in the docs...
The following solution worked for me:
# Imports
from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.StlAPI import StlAPI_Writer
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
input_file = 'stl_test.stp' # input STEP (AP203/AP214 file)
output_file = 'stl_test.stl' # output X3D file
### load stp file
step_reader = STEPControl_Reader()
stat = step_reader.ReadFile(input_file)
if stat == 1:
step_reader.TransferRoot()
body = step_reader.Shape()
else:
print('...........Load failed')
return 'Error'
print("File read")
### Convert and export body to STL File
mesh = BRepMesh_IncrementalMesh(body, 0.1)
writer = StlAPI_Writer()
writer.Write(mesh.Shape(), output_file)
print("Written")
Maybe it has something to do with the "AP203/AP214 file"?! I don't realy know.
Related
I'm trying to read in a .csv file and get this error
ValueError: could not convert string to float: '220'
Here's my code so far:
import csv
import matplotlib
import os
environment = []
# Initialise data dir
dir = os.getcwd()
print(dir)
parent = os.path.dirname(dir)
parent = os.path.dirname(parent)
parent = os.path.dirname(parent)
basedir = os.path.dirname(parent)
print(basedir)
datadir = os.path.join(basedir, 'data')
print(datadir)
inputdatadir = os.path.join(datadir, 'input')
print(inputdatadir)
# Open file and read.
file = os.path.join(inputdatadir, 'snowslope1.csv')
f = open(file, newline='')
reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
for row in reader: # A list of rows
rowlist = []
for value in row: # A list of value
rowlist.append(value)
#print(value)
environment.append(rowlist)
f.close()
# Plot environment.
matplotlib.pyplot.imshow(environment)
matplotlib.pyplot.show()
Can anybody help with what's going wrong here? I'm unsure where the error is as they all appear to be just numbers like this: 220,221,222,223 in my file snowslope1.csv
TIA
This looks like a UTF-8 BOM.
Try opening the file with encoding="utf-8-sig"
Hi I am trying to convert excel sheet to pdf using python, converted a script wrote to do same with word documents, which works fine but having this error below flagging up
Traceback (most recent call last):
File "C:/Users/alank/Python training/Exceltopdf2.py", line 13, in <module>
xlxs.SaveAs(out_file, FileFormat=xlxsFormatPDF)
OSError: exception: access violation reading 0xFFFFFFFFFFFFFFFF
any help appreciated and script is below
import sys
import os
import comtypes.client
xlxsFormatPDF = 17
in_file = (r'C:\Users\alank\Python training\Helloworld.xlsx')
out_file = (r'C:\Users\alank\Python training\Helloworld.pdf')
excel = comtypes.client.CreateObject('Excel.Application')
xlxs = excel.workbooks.Open(in_file)
xlxs.SaveAs(out_file, FileFormat=xlxsFormatPDF)
xlxs.Close()
excel.Quit()
You can try with win32com.client
like this:
import win32com.client
from pywintypes import com_error
WB_PATH = r'C:\Users\alank\Python training\Helloworld.xlsx'
PATH_TO_PDF = r'C:\Users\alank\Python training\Helloworld.pdf'
excel.Visible = False
try:
# Open
wb = excel.Workbooks.Open(WB_PATH)
# Specify the sheet you want to save by index.
#if you want all the sheets in excel try with:
#wb.WorkSheets(wb.Sheets.Count) or wb.WorkSheets([i=1 for i in range(wb.Sheets.Count)]).Select()
ws_index_list = [1,2,3,4,5,6,7,8,9,10,11,12]
wb.WorkSheets(ws_index_list).Select()
# Save
wb.ActiveSheet.ExportAsFixedFormat(0, PATH_TO_PDF)
except com_error as e:
print('The convertion failed.')
else:
print('Succeessful convertion')
finally:
wb.Close()
excel.Quit()
Or you can do it like here (Andreas solution):
import os
import comtypes.client
SOURCE_DIR = r'C:\Users\alank\Python training'
TARGET_DIR = r'C:\Users\alank\Python training'
app = comtypes.client.CreateObject('Excel.Application')
app.Visible = False
infile = os.path.join(os.path.abspath(SOURCE_DIR), 'Helloworld.xlsx')
outfile = os.path.join(os.path.abspath(TARGET_DIR), 'Helloworld.pdf')
doc = app.Workbooks.Open(infile)
doc.ExportAsFixedFormat(0, outfile, 1, 0)
doc.Close()
app.Quit()
I have spent the day trying to figure out how to export out a MS Excel File as a PDF. I am in desperate need of someone smarter than I:
Here is what I have so far and the error I get:
import os
import win32com.client
import win32com.client.dynamic
import datetime
import time
#
#Path to Read from where you want all the files read from
InputWkbkPath = "O:/MIS/Reporting/Field Reports/2014_Template_Files/w_code/"
obj = win32com.client.Dispatch("Outlook.Application")
xlApp = win32com.client.DispatchEx('Excel.Application')
OutputWkbkPath ='O:/MIS/Reporting/Field Reports/2015_Template_Files/Directors /Templates/20150123_Archive/'
for subdir, dirs, files in os.walk(InputWkbkPath):
for file in files:
#print os.path.join(subdir, file)
ip= os.path.join(subdir, file)
xlwb= xlApp.Workbooks.Open(ip)
#print xlwb
currentyear = datetime.date.today().strftime("%Y")
currentmonth = datetime.date.today().strftime("%B")
currentday = datetime.date.today().strftime("%d")
currentdate = currentmonth+"-"+currentday+"-"+currentyear
participant = xlwb.Worksheets(1).Range("C4").Value
title = xlwb.Worksheets(1).Range("C5").Value
StaffCode = xlwb.Worksheets(1).Range("C6").Value
OfficeName = xlwb.Worksheets(1).Range("C7").Value
LOCode = xlwb.Worksheets(1).Range("C8").Value
Region = xlwb.Worksheets(1).Range("C9").Value
ESN = str(xlwb.Worksheets(1).Range("C10").Value)
ParticipantEmail= xlwb.Worksheets(1).Range("C11").Value
MDEmail= xlwb.Worksheets(1).Range("C12").Value
RVPEmail = xlwb.Worksheets(1).Range("C13").Value
if title == "Director" or title == "DIRECTOR":
FileName = LOCode+"_"+participant+"_"+ESN+"_Comp_Model_"+currentdate+".xlsx"
#print FileName
else:
FileName = Region+"_"+LOCode+"_"+participant+"_"+ESN+"_Comp_Model_"+currentdate+".pdf"
OutputFile=OutputWkbkPath+FileName
xlwb.Worksheets(1).Activate
#print OutputFile
ws=xlwb.Worksheets(1)
ws.Visible = 1
xlwb.ExportAsFixedFormat(Type="xlTypePDF",OutputFile)
xlwb.Close(True)
I get the following error:
C:\Python27\python.exe C:/Users/username/PycharmProjects/File_Names/Loop_Throug_Open.py
File "C:/Users/username/PycharmProjects/File_Names/Loop_Throug_Open.py", line 55
xlwb.ExportAsFixedFormat(Type="xlTypePDF",OutputFile)
SyntaxError: non-keyword arg after keyword arg
Process finished with exit code 1
Please help. I can not find any on it in the groups.
Thank you ahead of time.
Robert
The problem was in the ExportAsFixedFormat method:
I changed to the following:
xlwb.ExportAsFixedFormat(0, OutputFile)
I also had to put the path with double regular slashes. So the outputWkbkPath looks like the following:
OutputWkbkPath ='O:\\MIS/Reporting\\Field Bonus Plan Reports\\2015__Files\\ DirectorsTemplates\\20150123_Archive\\'
I hope this helps someone else. The following post actually got me there:
.xlsx and xls(Latest Versions) to pdf using python
It is not the same package/module but that part works.
I need to setup some test conditions to simulate a filled up disk. I created the following to simply write garbage to the disk:
#!/usr/bin/python
import os
import sys
import mmap
def freespace(p):
"""
Returns the number of free bytes on the drive that ``p`` is on
"""
s = os.statvfs(p)
return s.f_bsize * s.f_bavail
if __name__ == '__main__':
drive_path = sys.argv[1]
output_path = sys.argv[2]
output_file = open(output_path, 'w')
while freespace(drive_path) > 0:
output_file.write("!")
print freespace(drive_path)
output_file.flush()
output_file.close()
As far as I can tell by looking at the return value from freespace, the write method does not write the file to until it is closed, thereby making the while condition invalid.
Is there a way I can write the data directly to the file? Or another solution perhaps?
This is untested but I imagine something along these lines will be the quickest way to fill the disk easily
import sys
import errno
write_str = "!"*1024*1024*5 # 5MB
output_path = sys.argv[1]
with open(output_path, "w") as f:
while True:
try:
f.write(write_str)
f.flush()
except IOError as err:
if err.errno == errno.ENOSPC:
write_str_len = len(write_str)
if write_str_len > 1:
write_str = write_str[:write_str_len/2]
else:
break
else:
raise
You could try/catch a disk full exception on write.
Is it possible to run a .html or .exe for example, that is inside a zipfile? I'm using the Zipfile module.
Here's my sample code:
import zipfile
z = zipfile.ZipFile("c:\\test\\test.zip", "r")
x = ""
g = ""
for filename in z.namelist():
#print filename
y = len(filename)
x = str(filename)[y - 5:]
if x == ".html":
g = filename
f = z.open(g)
After f = z.open(g), I don't know what to do next. I tried using the .read() but it only reads whats inside of the html, what I need is for it to run or execute.
Or is there any othere similar ways to do this?
The best approach will be to extract the required file to the Windows temp directory and execute it. I have modified your original code to create a temp file and execute it:
import zipfile
import shutil
import os
z = zipfile.ZipFile("c:\\test\\test.zip", "r")
x = ""
g = ""
basename = ""
for filename in z.namelist():
print filename
y = len(filename)
x = str(filename)[y - 5:]
if x == ".html":
basename = os.path.basename(filename) #get the file name and extension from the return path
g = filename
print basename
break #found what was needed, no need to run the loop again
f = z.open(g)
temp = os.path.join(os.environ['temp'], basename) #create temp file name
tempfile = open(temp, "wb")
shutil.copyfileobj(f, tempfile) #copy unzipped file to Windows 'temp' folder
tempfile.close()
f.close()
os.system(temp) #run the file
Run the first .html file in a zip archive specified at the command line:
#!/usr/bin/env python
import os
import shutil
import sys
import tempfile
import webbrowser
import zipfile
from subprocess import check_call
from threading import Timer
with zipfile.ZipFile(sys.argv[1], 'r') as z:
# find the first html file in the archive
member = next(m for m in z.infolist() if m.filename.endswith('.html'))
# create temporary directory to extract the file to
tmpdir = tempfile.mkdtemp()
# remove tmpdir in 5 minutes
t = Timer(300, shutil.rmtree, args=[tmpdir], kwargs=dict(ignore_errors=True))
t.start()
# extract the file
z.extract(member, path=tmpdir)
filename = os.path.join(tmpdir, member.filename)
# run the file
if filename.endswith('.exe'):
check_call([filename]) # run as a program; wait it to complete
else: # open document using default browser
webbrowser.open_new_tab(filename) #NOTE: returns immediately
Example
T:\> open-from-zip.py file.zip
As an alternative to webbrowser you could use os.startfile(os.path.normpath(filename)) on Windows.