我将数据框(df)作为新标签(“打印”)添加到现有的excel文件中。我在调整列宽时遇到困难吗?
代码
book = load_workbook('file.xlsx')
writer = pd.ExcelWriter('file.xlsx', engine = 'openpyxl')
writer.book = book
df.to_excel(writer,sheet_name = 'Print')
worksheet = writer.sheets['Print']
worksheet.set_column('B:B', 40) #This does not work
答案 0 :(得分:0)
writer = pd.ExcelWriter('file.xlsx', engine='openpyxl')
writer.book = book
df.to_excel(writer, sheet_name='Print')
sheet = book.get_sheet_by_name('Print')
sheet.column_dimensions['B'].width = 40
writer.save()
答案 1 :(得分:-2)
我从没有使用过,也没有做过,但是我只是在Google上搜索“ worksheet.set_column”并发现了这一点: https://xlsxwriter.readthedocs.io/worksheet.html
该函数的语法为
set_column(first_col, last_col, width, cell_format, options)
所以我想说的答案是:
worksheet.set_column(2, 2, 40)