I want to automize my routine tasks such as changing 1 word in huge AutoCAD .DWG file with a lot of text blocks (objects in autocad). So, can I open it by Python? And how?
Asked
Active
Viewed 2,779 times
0
-
1with open('yourfile.dwg', 'r') as f: this way you have it open for reading, check python.org for more documentation – Drako Oct 05 '18 at 11:37
-
@Drako "No results found." after search on python.org "dwg". – Artem Getmanskiy Oct 05 '18 at 11:38
-
@FlyingTeller no! dwg isn't common file for reading. – Artem Getmanskiy Oct 05 '18 at 11:39
-
@FlyingTeller blocks of text in dwg, it's an objects, but i don't know what kind of and how to parse it. I now Python, and, ofc, know open() function :) – Artem Getmanskiy Oct 05 '18 at 11:43
-
Drako is right, that line *will open the file*. But if you need to know how to parse it, you must investigate that on your own. Nobody is going to write a full file format parser for you. – Jongware Oct 05 '18 at 11:44
-
Looks like `dwg` does not have public specifications, so you will probably not find any freely available library to open it – FlyingTeller Oct 05 '18 at 11:44
-
@FlyingTeller okey, thank u! – Artem Getmanskiy Oct 05 '18 at 11:47
-
@usr2564301 Thanks for comment! – Artem Getmanskiy Oct 05 '18 at 11:47
-
open it binary and replace - should work fine, but needs testing – Drako Oct 05 '18 at 11:55
1 Answers
3
Your best bet here is to convert the file first into something like SVG or .dxf and go from there. It's not easy getting the data from a .dwg file in python.
You can make the conversion to SVG in python too using subprocess
and a command line convert tool like cad2svg.

Nordle
- 2,915
- 3
- 16
- 34
-
Thanks! I've found info 'bout SVG. Can u give me the way 'bout working with .dxf for further research, plz? – Artem Getmanskiy Oct 05 '18 at 11:46
-
Take a look at this convertor - https://www.opendesign.com/guestfiles/TeighaFileConverter to make the conversion, and within python you could look to the `dxfgrabber` library to handle the file. – Nordle Oct 05 '18 at 11:48
-