Centering Text while using Python Code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
So I have been developing a code below that will search a specific folder for dwg's then it will open them and edit some revision text. Here is the code below (I will explain the prob after the code):
import os
import win32com.client
import time
# ----------------------Set global Type Variables For this Program---------------------------------#
# File Extension of the file type we want to sort out of the active directory
file_extension = "dwg"
# Location of the source folder
path = 'C:/Userbackups/autocad-sample-drawings/'
save_path = 'C:/Userbackups/autocad-sampleRev-Up-drawings/'
# ----------------------Get a list of all the .dwg files in directory path---------------------------#
dir_list = os.listdir(path)
dwg_files = [file for file in dir_list if file.split('.')[1] == file_extension]
# Debugging check display the files on the terminal
# Specify the directory where the files are located
print("Files and directories in '", path, " :")
# List the files on the screen
for filename in dwg_files:
print(filename)
# Add a blank row on the screen
print("\n")
# Open the AutoCAD
acad = win32com.client.dynamic.Dispatch("AutoCAD.Application")
# Go through each file in the directory that had a .dwg extension
for filename in dwg_files:
# Define the file path and file name to open
file_path = path + filename
# For debugging confirm file path
print(file_path)
# Open the file as an AutoCad object
time.sleep(5)
doc = acad.Documents.Open(file_path)
# Make the autocad application visible on the screen
time.sleep(3)
acad.Visible = True
# ---------------------- AutoCad prompt commands ------------------------------------------------#
# 5 Second delay added for now to ensure Autocadd open the drawing
print('5 second delay start')
time.sleep(5)
print('5 second delay end')
# Send Zoom Extents Command to AutoCad
doc.SendCommand("_ZOOM _E \n")
# Change the Rev Number on the drawing
doc.SendCommand('\n')
doc.SendCommand('_GATTE _B Ttl_b #5 B \n Y \n')
doc.SendCommand('\n')
# Add new rev #
doc.SendCommand('\n')
doc.SendCommand('_GATTE _B Ttl_b #30 B \n Y \n')
doc.SendCommand('\n')
# Add new rev Month
doc.SendCommand('\n')
doc.SendCommand('_GATTE _B Ttl_b #31 OCT \n Y \n')
doc.SendCommand('\n')
# Add new rev YEAR
doc.SendCommand('\n')
doc.SendCommand('_GATTE _B Ttl_b #32 23 \n Y \n')
doc.SendCommand('\n')
# Add new rev DESCRIPTION TOP
doc.SendCommand('\n')
doc.SendCommand('_GATTE _B Ttl_b #33 IFC \n Y \n')
doc.SendCommand('\n')
# Add new rev DESCRIPTION BOTTOM
doc.SendCommand('\n')
doc.SendCommand('_GATTE _B Ttl_b #34 PROJ#-I&E=#1 \n Y \n')
doc.SendCommand('\n')
# Add new rev DRAWN BY
doc.SendCommand('\n')
doc.SendCommand('_GATTE _B Ttl_b #35 TC \n Y \n')
doc.SendCommand('\n')
# Add new rev CHECKED BY
doc.SendCommand('\n')
doc.SendCommand('_GATTE _B Ttl_b #36 TC \n Y \n')
doc.SendCommand('\n')
# Add delay to ensure changes made
time.sleep(3)
save_file_path = save_path + filename
doc.SaveAs(save_file_path)
doc.Close()
print(filename, " Rev'd up\n")
# Close AutoCad
time.sleep(3)
acad.Quit()
print("Rev up completed")So the code will input the text fine but it will input the text at an off center alignment. See photo below
The Line with the "A" revision is manually entered text - When manually entered the text appears in the center of the text box. The properties of the text says the alignment is "Center Middle"
The Line with the "B" revision is text entered using the code - When entered with the code the text appears in the left side of the text box. The properties of the text still says the alignment is "Center Middle".
Anyone have any ideas how to automate the process of having this text auto centered within the code?