【发布时间】:2020-10-29 15:00:45
【问题描述】:
我有一个正在运行的 python 脚本,它导入我的同事每天更新的一个 xlsx 文件。我的脚本在任务计划程序上运行并拉入此 Excel 文件,读取多个工作表,设置标题名称,然后将每个工作表导出到与我们组织中的其他人共享的单个文件夹和 Excel 文件。
我遇到的问题是新的 xlsx 文件列根本没有大小。理想情况下,我希望它们在所有列中设置为一定的宽度,以便于使用/可读性。我一直在寻找一种方法来阅读多张纸并在 openpyxl 和 writer 中使用列格式导出每张纸。有人有任何可能有用的想法吗?感谢您的帮助!
代码部分:多张工作表的其余代码看起来相同...
import pandas as pd
from pandas import option_context
## List column names for 15 and 16 column sheets
gt_cols16 = ['Patient Name', 'Move In Date', 'Addendum Paperwork', 'Consent Paperwork', 'Authorization Paperwork', 'Paperwork Mailed', 'SOC Pioneer IHP', 'SOC HH', 'SOC Hospice', 'SOC OP', 'SOC Pioneer PCP', 'Plan of Care Sent', 'Covid 19 Positive', 'Covid 19 Last Test Date', 'Would have moved out without services?', 'Notes/Story']
gt_cols15 = ['Patient Name', 'Move In Date', 'Addendum Paperwork', 'Consent Paperwork', 'Authorization Paperwork', 'SOC Pioneer IHP', 'SOC HH', 'SOC Hospice', 'SOC OP', 'SOC Pioneer PCP', 'Plan of Care Sent', 'Covid 19 Positive', 'Covid 19 Last Test Date', 'Would have moved out without services?', 'Notes/Story']
##Define Excel to use
xls = pd.ExcelFile('Grace Tracking.xlsx')
##Generate Data frames from Excel File xls, split each sheet and write to excel file
df1 = pd.read_excel(xls, sheet_name='State St Memory Care')
df1.columns = gt_cols16
df1 = df1.iloc[1:]
df1.to_excel('State_St_Memory_Care\State_St_Memory_Care.xlsx', index=False)
df2 = pd.read_excel(xls, sheet_name='State St IL')
df2.columns = gt_cols15
df2 = df2.iloc[1:]
df2.to_excel('State_St_IL\State_St_IL.xlsx', index=False)
df3 = pd.read_excel(xls, sheet_name='State St AL')
df3.columns = gt_cols16
df3 = df3.iloc[1:]
df3.to_excel('State_St_AL\State_St_AL.xlsx', index=False)
【问题讨论】: