# this program writes the tags present in a DICOM file to a text file. Repeats for all DICOMs in that folder. # 2020-09-01-0859 import codecs import sys from pathlib import Path import glob import pydicom from pydicom.filereader import dcmread import os sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer) CWD = os.getcwd() with open ('list_of_tags.txt', 'w+',encoding='utf-8') as txtfile: #file_root = 'your path' #file_root = 'your path' file_root = CWD dcm_path = file_root+'/*.dcm' for filename in glob.glob(dcm_path): try: dataset = dcmread(filename) file_lst = filename.split('\\') txtfile.write(file_lst[-1] + "---------------------------------------------------------\n") #writes filename and visible separator to text file for me in dataset.file_meta: # gets meta tags (read-only) dataset.file_meta.me = '00' txtfile.write(" " + str(me) + '\n') for de in dataset: line = str(de) filePlusline = file_lst[-1] + " " + line linelst = filePlusline.split() wanted_txt = str(linelst) txtfile.write(" "+ line + "\n") # writes the individual tag with 5 preceding spaces #txtfile.write(" "+file_list[-1] + " " + line + "\n") # use this line instead of the previous line if you want the file name before each tag #txtfile.write("\n") #creates a blank line between files print("{} completed".format(file_lst[-1])) except: print("\n*****\nERROR: {}\n*****\n".format(filename))