程序实现: 读取当前目录以及子文件夹内的dxf文件,首先会把图块分解成线条,然后参照3.xlsx内的原文以及译文进行翻译写入(3.xlsx文件第一列保存原文,第二列保存译文)。
编程技术技术有限,有一些功能没有实现。
进行翻译之前需要自己将dwg图纸转换成dxf图纸,翻译好之后也需要自己再转换成dwg文件(仅尝试过2010版本文件,2018版本貌似会乱码)
另外附一个能从dxf文件中提取原文的程序,以便翻译。
实现翻译的的代码
import os
import pandas as pd
import ezdxf
from pathlib import Path
def load_translation_map(excel_path):
df = pd.read_excel(excel_path)
return dict(zip(df.iloc[:, 0], df.iloc[:, 1]))
def translate_text_entity(msp, entity, translation_map):
original_text = entity.dxf.text
translated_text = translation_map.get(original_text)
if translated_text:
msp.add_text(translated_text, dxfattribs={
'insert': (entity.dxf.insert[0], entity.dxf.insert[1] - 0.75),
'height': 1, # 设置字体大小
'color': 1 # 设置颜色为红色
})
def translate_dwg(dwg_path, translation_map):
doc = ezdxf.readfile(dwg_path)
msp = doc.modelspace()
# 翻译模型空间中的文本
for entity in msp.query('TEXT MTEXT'):
translate_text_entity(msp, entity, translation_map)
# 翻译块中的文本
for block in doc.blocks:
for entity in block.query('TEXT MTEXT'):
translate_text_entity(msp, entity, translation_map)
return doc
def process_directory(directory, translation_map, output_folder):
for root, dirs, files in os.walk(directory):
# 跳过translated_drawings文件夹
dirs[:] = [d for d in dirs if d != output_folder.name]
for name in files:
if name.endswith('.dxf'):
file_path = Path(root) / name
relative_path = file_path.relative_to(directory)
output_path = output_folder / relative_path
output_path.parent.mkdir(parents=True, exist_ok=True)
print(f"Processing file: {file_path}")
doc = translate_dwg(file_path, translation_map)
doc.saveas(str(output_path))
print(f"File saved: {output_path}")
def main():
translation_map = load_translation_map('3.xlsx')
source_folder = Path('.') # Current directory
output_folder = Path('translated_drawings')
output_folder.mkdir(exist_ok=True)
process_directory(source_folder, translation_map, output_folder)
if __name__ == "__main__":
main()
实现提取原文的代码:
import os
import pandas as pd
import ezdxf
from pathlib import Path
def extract_text_from_dxf(file_path):
try:
doc = ezdxf.readfile(file_path)
msp = doc.modelspace()
texts = []
# 提取模型空间中的文本
for entity in msp.query('TEXT MTEXT'):
texts.append(entity.dxf.text)
# 提取块中的文本
for block in doc.blocks:
for entity in block.query('TEXT MTEXT'):
texts.append(entity.dxf.text)
return texts
except IOError:
print(f"Could not read file: {file_path}")
return []
except ezdxf.DXFStructureError:
print(f"Invalid or corrupted DXF file: {file_path}")
return []
def process_directory(directory):
all_texts = []
for root, dirs, files in os.walk(directory):
for name in files:
if name.endswith('.dxf'):
file_path = Path(root) / name
texts = extract_text_from_dxf(file_path)
for text in texts:
all_texts.append([file_path, text])
return all_texts
def main():
directory = Path('.') # 当前目录
texts = process_directory(directory)
# 将提取的文本保存到Excel文件中
df = pd.DataFrame(texts, columns=['File Path', 'Text'])
df.to_excel('extracted_texts.xlsx', index=False)
print("Texts have been extracted and saved to 'extracted_texts.xlsx'.")
if __name__ == "__main__":
main()