Skip to content

Instantly share code, notes, and snippets.

@V0xP0p
Created May 22, 2020 07:54
Show Gist options
  • Save V0xP0p/55aa064de912996f5fa521724bf4bba9 to your computer and use it in GitHub Desktop.
Save V0xP0p/55aa064de912996f5fa521724bf4bba9 to your computer and use it in GitHub Desktop.
A simple code to parse an AutoCAD file for specific text values and export them in an excel file
from pyautocad import Autocad
import pandas as pd
import tkinter as tk
from tkinter import filedialog
# create two lists to hold the accessories and profiles strings
accessories = []
profiles = []
# check if an autocad object exists if not open autocad
acad = Autocad(create_if_not_exists=True)
# iterate through all objects in the current drawing and filter the text objects
for text in acad.iter_objects('tex'):
str = text.TextString
stlen = len(str)
# check for text objects that fit the naming of accessories or profiles
if stlen == 10 and str.isnumeric():
accessories.append(str)
elif stlen <= 6 and (str.startswith('M') or str.startswith('S')) and str[1:].isnumeric():
profiles.append(str)
# pass the data to dataframes and create new dataframes with the unique values present
data1 = pd.DataFrame({'Accessories': accessories})
df1 = pd.DataFrame({'Accessories': data1['Accessories'].unique()})
data2 = pd.DataFrame({'Profiles': profiles})
df2 = pd.DataFrame({'Profiles': data2['Profiles'].unique()})
root = tk.Tk()
root.withdraw()
# define the available types to be present on the file dialog
ftypes = [('Excel files', '.xlsx'), ('All files', '*')]
# open a file dialog to select path for the excel output
file_path = filedialog.asksaveasfilename(filetypes=ftypes, defaultextension='.xlsx')
# export the data to an excel file in different sheets
with pd.ExcelWriter(file_path) as writer:
df1.to_excel(writer, sheet_name='Accessories')
df2.to_excel(writer, sheet_name='Profiles')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment