Convert Excel To Xrdml High Quality -
Excel is a spreadsheet tool; it stores data in rows and columns but lacks the specific hierarchy required for scientific instrumentation. An XRDML file is structured—it tells the software:
If you try to force raw numbers into analysis software, it often misinterprets the scale or loses the step-size calibration, leading to incorrect peak identification.
Subject: How to convert Excel data to XRDML while maintaining high quality
Text: Converting XY data from Excel to the XML-based XRDML format requires strict adherence to the schema definitions. To achieve a high-quality conversion, the process involves mapping spreadsheet columns to specific XML nodes (Start/End positions, Step size, and Intensities). Our method eliminates the common pitfalls of data truncation and header corruption, producing a robust XRDML file that retains the original resolution and provenance of your experiment. convert excel to xrdml high quality
from xrdmllib import XRDMLDocument doc = XRDMLDocument(data=df['intensity'].values, two_theta=df['tt'].values, metadata=meta) doc.save('converted_high_quality.xrdml')
Quality Rating: ⭐⭐⭐⭐ Pros: Free, automated, no data smoothing. Cons: Requires coding knowledge; manual metadata entry.
import pandas as pd
import xml.etree.ElementTree as ET
from xml.dom import minidom
from datetime import datetime
def excel_to_xrdml(excel_path, sheet_name=0, two_theta_col='2θ', intensity_col='Intensity',
output_path=None, xray_wavelength=1.5406, step_size=None):
"""
Convert Excel XRD data to high-quality .xrdml file (PANalytical format) Excel is a spreadsheet tool; it stores data
Parameters:
- excel_path: path to Excel file
- sheet_name: sheet name or index (default 0)
- two_theta_col: column name for 2θ values
- intensity_col: column name for intensity values
- output_path: output .xrdml path (default: same name with .xrdml)
- xray_wavelength: Cu Kα wavelength in Å (default 1.5406)
- step_size: step size in degrees (auto-calculated if None)
"""
# Read Excel
df = pd.read_excel(excel_path, sheet_name=sheet_name)
two_theta = df[two_theta_col].values
intensities = df[intensity_col].values
# Calculate step size if not provided
if step_size is None and len(two_theta) > 1:
step_size = round(two_theta[1] - two_theta[0], 6)
else:
step_size = step_size or 0.02
start_angle = two_theta[0]
end_angle = two_theta[-1]
total_points = len(two_theta)
# Create root element
root = ET.Element('xrdml',
'xmlns': 'http://www.xrdml.com/XRDML',
'version': '1.0'
)
# === Measurement (metadata) ===
measurement = ET.SubElement(root, 'measurement')
# Date
date_elem = ET.SubElement(measurement, 'date')
date_elem.text = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
# Instrument
instrument = ET.SubElement(measurement, 'instrument')
ET.SubElement(instrument, 'name').text = 'Converted from Excel'
# Source (X-ray tube)
source = ET.SubElement(instrument, 'source')
ET.SubElement(source, 'type').text = 'X-ray tube'
ET.SubElement(source, 'anode').text = 'Cu'
ET.SubElement(source, 'wavelength', 'unit': 'Å').text = str(xray_wavelength)
# Gonio (geometry)
gonio = ET.SubElement(instrument, 'gonio')
ET.SubElement(gonio, 'geometry').text = 'Bragg-Brentano'
# Detector
detector = ET.SubElement(instrument, 'detector')
ET.SubElement(detector, 'type').text = 'Scintillation'
# === Data collection parameters ===
data_collection = ET.SubElement(measurement, 'dataCollection')
ET.SubElement(data_collection, 'startAngle', 'unit': 'deg').text = str(start_angle)
ET.SubElement(data_collection, 'endAngle', 'unit': 'deg').text = str(end_angle)
ET.SubElement(data_collection, 'stepSize', 'unit': 'deg').text = str(step_size)
ET.SubElement(data_collection, 'totalPoints').text = str(total_points)
# === The actual XRD scan data ===
scan = ET.SubElement(root, 'scan')
# Points with intensities
points = ET.SubElement(scan, 'points')
points.text = ','.join([f'i:.1f' for i in intensities])
# Positions (2θ values) — optional but good for completeness
positions = ET.SubElement(scan, 'positions', 'unit': 'deg')
positions.text = ','.join([f'ang:.5f' for ang in two_theta])
# Optional: add comments
comment = ET.SubElement(root, 'comment')
comment.text = f'Converted from excel_path on datetime.now().strftime("%Y-%m-%d")'
# Make pretty XML
xml_str = ET.tostring(root, encoding='unicode')
dom = minidom.parseString(xml_str)
pretty_xml = dom.toprettyxml(indent=' ')
# Remove extra newlines before root (minidom adds one)
pretty_xml = pretty_xml[pretty_xml.find('<?xml'):] if '<?xml' in pretty_xml else pretty_xml
# Save
if output_path is None:
output_path = excel_path.replace('.xlsx', '.xrdml').replace('.xls', '.xrdml')
with open(output_path, 'w', encoding='utf-8') as f:
f.write(pretty_xml)
print(f'✅ High-quality .xrdml saved to: output_path')
print(f' Points: total_points, Range: start_angle–end_angle°, Step: step_size°')
return output_path
Several software tools and online converters can directly convert Excel files to XRDML. Some popular options include:
These tools are convenient and often user-friendly, but may have limitations on file size, formatting, or data complexity.
Advanced Convert
Batch/Folder Convert (CLI or API)
Integration with Lab Systems
Before we discuss the how, we must address the why. A "low quality" XRDML file is worse than useless—it risks invalidating your phase identification. If you try to force raw numbers into
Provide a reliable, high-quality conversion tool that turns tabular X-ray diffraction (XRD) data in Excel into valid XRDML files suitable for analysis and instrumentation software.