excel - 自动化多个 Excel 文件的单元格属性
问题描述
我有大约 100 个 Excel 文件,其中列 K 和 L 表示为浮点数,例如 0.5677。我想将这些列表示为百分比,在本例中为 56.8%。有没有办法可以自动化这个?显然我可以手动调整列,但这非常耗时。
我没有使用宏或 VBA 的经验。
任何帮助将不胜感激。
亲切的问候,M。
解决方案
我找到了一种使用 Python Pandas 的有用方法。就我而言,Pandas 也是 Excel 文件的来源。
import pandas as pd
# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Numbers': [1010, 2020, 3030, 2020, 1515, 3030, 4545],
'Percentage': [.1, .2, .33, .25, .5, .75, .45 ],
})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter("pandas_column_formats.xlsx", engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Add some cell formats.
format1 = workbook.add_format({'num_format': '#,##0.00'})
format2 = workbook.add_format({'num_format': '0%'})
# Note: It isn't possible to format any cells that already have a format such
# as the index or headers or any cells that contain dates or datetimes.
# Set the column width and format.
worksheet.set_column('B:B', 18, format1)
# Set the format but not the column width.
worksheet.set_column('C:C', None, format2)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
推荐阅读
- wagtail - HTML 模板中的 wagtail 页面模型参考
- c - 在 Visual GDB 中,如何在变量更改时设置中断?
- regex - VS Code Snippets 对单个变量进行多次转换
- javascript - ReactJs 按钮 onClick 未触发且输入字段不显示任何内容
- kotlin - 使用外键而不是实体 ID 引用的实体
- c++ - 为什么 make_shared 调用移动构造函数两次?如何避免?
- r - 在 R 中使用 Keras 包的 MLP 模型无法学习(高训练和 val_accuracy 但在测试数据上的性能非常差)
- javascript - 无法在 node.js 中创建子进程
- tensorflow - 使用 tf.data.Dataset.from_tensor_slices(....) 上的地图标记数据集
- angular - 无法重置的输入字段
底角的