gpt4 book ai didi

python - Pandas 卡住列名

转载 作者:行者123 更新时间:2023-12-04 22:11:13 25 4
gpt4 key购买 nike

当我向下滚动以始终拥有列名时,我想卡住大文件中的列名和类似内容。
这是我的脚本,我也在其中创建具有多个 excel 作为工作表的 excel 文件。

import numpy as np
import pandas as pd
from timestampdirectory import createdir
import openpyxl
import xlsxwriter
from openpyxl import workbook
from openpyxl import worksheet

import os
import time
def svnanalysis():

dest = createdir()
dfSvnUsers = pd.read_excel(os.path.join(dest, "SvnUsers.xlsx"))
dfSvnUsers.fillna("N/A", inplace=True)
dfSvnGroupMembership = pd.read_excel(os.path.join(dest, "SvnGroupMembership.xlsx"))
dfSvnRepoGroupAccess = pd.read_excel(os.path.join(dest, "SvnRepoGroupAccess.xlsx"))
dfsvnReposSize = pd.read_excel(os.path.join(dest, "svnReposSize.xlsx"))
dfsvnRepoLastChangeDate = pd.read_excel(os.path.join(dest, "svnRepoLastChangeDate.xlsx"))
dfUserDetails = pd.read_excel(r"D:\GIT-files\Automate-Stats\SVN_sample_files\CM_UsersDetails.xlsx")
dfUserDetails.fillna("N/A", inplace=True)

timestr = time.strftime("%Y-%m-%d-")
xlwriter = pd.ExcelWriter(os.path.join(dest,f'{timestr}Usage-SvnAnalysis.xlsx'))

dfUserDetails.to_excel(xlwriter, sheet_name='UserDetails',index = False)
dfSvnUsers.to_excel(xlwriter, sheet_name='SvnUsers', index = False )
dfSvnGroupMembership.to_excel(xlwriter, sheet_name='SvnGroupMembership', index = False )
dfSvnRepoGroupAccess.to_excel(xlwriter, sheet_name='SvnRepoGroupAccess', index = False)
dfsvnReposSize.to_excel(xlwriter, sheet_name='svnReposSize', index = False)
dfsvnRepoLastChangeDate.to_excel(xlwriter, sheet_name='svnRepoLastChangeDate',index= False)

for column in dfSvnUsers:
column_width = max(dfSvnUsers[column].astype(str).map(len).max(), len(column))
col_idx = dfSvnUsers.columns.get_loc(column)
xlwriter.sheets['SvnUsers'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['UserDetails'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['SvnGroupMembership'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['SvnRepoGroupAccess'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['svnReposSize'].set_column(col_idx, col_idx, column_width)
xlwriter.sheets['svnRepoLastChangeDate'].set_column(col_idx, col_idx, column_width)

# xlwriter.freeze_columns(1, 0) # # Freeze the first row.

xlwriter.close()

#usage = pd.read_excel(os.path.join(dest,f'{timestr}Usage-SvnAnalysis.xlsx'))
#usage.style.set_table_styles([
# {'selector': 'thead th', 'props': 'position: sticky; top:0; background-color:red;'},
# {'selector': 'tbody th', 'props': 'position: sticky; left:0; background-color:green;'}
#]).to_html()

print("UsageSvnAnalysis.xlsx a fost exportat cu succes continand ca sheet toate xlsx anterioare")
svnanalysis()
在带有“#”(注释行)的脚本末尾,它是我尝试过的,在带有 table_styles 的部分......所有工作但它没有卡住或更改每种颜色第一行的颜色(列名)
这是导出的 Excel:
基本上现在当我向下滚动时,列名应该总是出现并具有“蓝色”背景,但正如我所说的,# 代码行一切正常,但它没有应用 idk 为什么
    for dfSvnUsers in xlwriter.sheets:
ws = xlwriter.sheets['SvnUsers']
ws.freeze_panes(1, 0)

ws.style.set_table_styles([
{'selector': 'thead th', 'props': 'position: sticky; top:0; background-color:red;'},
{'selector': 'tbody th', 'props': 'position: sticky; left:0; background-color:green;'}
]).to_html()
我更新了脚本,现在工作并卡住具有特定工作表的列名的第一行,但我也尝试将背景的颜色更改为“红色”并且没有工作,
colorchange

最佳答案

尝试:

with pd.ExcelWriter(Your_FilePath, engine='xlsxwriter') as writer:
dfUserDetails.to_excel(xlwriter, sheet_name='UserDetails',index = False)

# etc etc ...

for sht_name in writer.sheets:
ws = writer.sheets[sht_name]
ws.freeze_panes(1, 0)

print("UsageSvnAnalysis.xlsx a fost exportat cu succes continand ca sheet toate xlsx anterioare")
svnanalysis()

关于python - Pandas 卡住列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72727251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com