Python+XlsxWriter循环复用单元格代码设置格式的问题咨询
Hey there! Great job getting your basic XlsxWriter code working—let's make it cleaner and reusable with loops while adding those bold/underline formats you want.
First, let's break this down into simple, easy-to-follow steps:
- Define your formats upfront using XlsxWriter's
add_format()method. This keeps all your style definitions organized in one spot. - Create a list of cell details that includes the cell address, text content, and the format (if any) you want to apply. This lets you handle all write operations in a single loop.
- Loop through the list to write each cell with its corresponding format.
Here's the complete code example:
# First, define your formats (make sure you've already created your workbook object) bold = workbook.add_format({'bold': True}) underline = workbook.add_format({'underline': True}) # Create a list where each item is a tuple containing (cell_address, text, format) # Use None for cells that don't need a special format cell_data_bold = [ ('A2', 'Scan Creation Date:', bold), ('A3', 'Total Hosts Scanned:', bold), ('A5', 'OVERALL FIGURES', bold), ('A6', 'Total Sev 5:', bold), ('A7', 'Total Sev 4:', None), ('A8', 'Total Sev 3:', None), ('A9', 'Total Sev 2:', None), ('A10', 'Total Sev 1:', None), ('A11', 'Overall Total:', bold), ('A13', 'Hostname', bold), ('B13', 'IP Address', None), ('C13', 'Sev 5', bold), ('D13', 'Sev 4', None), ('E13', 'Sev 3', None), ('F13', 'Sev 2', None), ('G13', 'Sev 1', None), ('H13', 'Total', None), ('I13', 'Stats (%)', bold), ] # If you need the underline variant (e.g., for a separate sheet or version), make another list cell_data_underline = [ ('A2', 'Scan Creation Date:', underline), ('A3', 'Total Hosts Scanned:', None), ('A5', 'OVERALL FIGURES', underline), ('A6', 'Total Sev 5:', None), ('A7', 'Total Sev 4:', None), ('A8', 'Total Sev 3:', None), ('A9', 'Total Sev 2:', None), ('A10', 'Total Sev 1:', None), ('A11', 'Overall Total:', underline), ('A13', 'Hostname', None), ('B13', 'IP Address', None), ('C13', 'Sev 5', None), ('D13', 'Sev 4', None), ('E13', 'Sev 3', None), ('F13', 'Sev 2', None), ('G13', 'Sev 1', None), ('H13', 'Total', None), ('I13', 'Stats (%)', None), ] # Now loop through the list to write all cells for the bold variant for cell, text, fmt in cell_data_bold: worksheet2.write(cell, text, fmt) # If you need to write the underline version (e.g., to a different worksheet), use: # for cell, text, fmt in cell_data_underline: # worksheet3.write(cell, text, fmt)
Why this works better than your earlier zip attempt:
When you used zip, you likely weren't including the format parameter as part of your iterable. By packing all three pieces of information (cell, text, format) into a single tuple in a list, you can easily unpack them in the loop and pass them directly to write(). Using None for cells without special formats keeps the loop consistent—XlsxWriter will automatically use the default format when you pass None.
This approach also makes your code way easier to maintain: if you need to add more cells or adjust formats later, you just update the list instead of editing dozens of individual write() lines.
内容的提问来源于stack exchange,提问作者fusion1275

