Python;在不更改数据的情况下为现有Excel文件上的电子邮件页面添加页眉/页脚

vuktfyat  于 2023-11-15  发布在  Python
关注(0)|答案(1)|浏览(148)

我正在尝试为现有Excel文件的所有工作表添加打印头标题。
我写的代码如下图所示,但它创建新的工作表和所有数据将丢失。
我如何添加一个页眉/页脚为一个现有的Excel文件的电子邮件页面,而不改变数据?

  1. import xlsxwriter
  2. import pandas as pd
  3. import glob
  4. import os
  5. def addExcelFile(locationPath, textAdd):
  6. for file in glob.glob(locationPath + '\\**\\*.xlsx', recursive=True):
  7. errorFile = os.path.basename(file).split('/')[-1]
  8. print(errorFile[0:2])
  9. if (errorFile[0:2] != '~$'):
  10. print(file)
  11. xl = pd.ExcelFile(file, engine='openpyxl')
  12. # Open a Pandas Excel writer using XlsxWriter as the engine.
  13. writer = pd.ExcelWriter(file, engine='xlsxwriter')
  14. # Get the xlsxwriter workbook and worksheet objects.
  15. workbook = writer.book
  16. listSheet = xl.sheet_names
  17. print(len(listSheet))
  18. # see all sheet names
  19. if(textAdd == 'LGE Confidential'):
  20. for i in range (0,len(listSheet)):
  21. nameSheet = listSheet[i]
  22. writer.sheets={nameSheet:workbook.add_worksheet()}
  23. worksheet = writer.sheets[nameSheet]
  24. # Set color for text :https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.colors?view=windowsdesktop-7.0
  25. # replace char FF -> K in start color code
  26. worksheet.set_header('&C&"Tahoma,Bold"&14&Kff0000' + textAdd)
  27. elif(textAdd == 'LGE Internal Use Only'):
  28. for i in range (0,len(listSheet)):
  29. nameSheet = listSheet[i]
  30. writer.sheets={nameSheet:workbook.add_worksheet()}
  31. worksheet = writer.sheets[nameSheet]
  32. # Set color for text :https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.colors?view=windowsdesktop-7.0
  33. # replace char FF -> K in start color code
  34. worksheet.set_header('&C&"Tahoma,Bold"&14&Ka9a9a9' + textAdd)
  35. # Close the Pandas Excel writer and output the Excel file.
  36. writer.save()
  37. xl.close()
  38. else:
  39. print('File error: ', file, '. It will not be add header!')

字符串
添加一个页眉/页脚为一个现有的excel文件的电子邮件页面,不改变数据。

oknwwptz

oknwwptz1#

你似乎做的比你所需要的结果要多。
你只需要在工作簿中的工作表中添加标题。没有必要使用Pandas和Xlwriters,只需使用Openpyxl并更新每个工作表。
你也有重复的代码,尽量避免重复的代码,如果同一个代码段可以用于多个应用程序。部分if(textAdd == 'LGE Confidential'):等重复约4行不必要的。
使用Openpyxl,你可以将页眉(和页脚)设置为左,中,右三个部分之一,并允许对第一页和奇数/偶数页进行不同的设置。在示例中,我使用了“中心”部分,并包括所有页面,这样你就可以改变为适合的页面。
据我所知,下面的代码示例应该能满足你的需要。我已经硬设置了'textAdd'字符串,所以代码是可运行的,在每个测试之间进行更改。

  1. import openpyxl
  2. import glob
  3. import os
  4. def addExcelFile(textAdd, workbook):
  5. ### Set the colour for the header, only two variations based on the text. Grey can be the
  6. ### default colour which is set and only changed if the text is 'LGE Confidential'
  7. head_color = 'a9a9a9' # Default to Grey
  8. if textAdd == 'LGE Confidential':
  9. head_color = 'ff0000' # Change to RED if the text is 'LGE Confidential'
  10. ### Create the Header from the default style, colour and text
  11. headertext = '&C&"Tahoma,Bold"&14&K' + head_color + textAdd
  12. ### Add the header as needed
  13. for sheet in workbook.worksheets:
  14. sheet.firstHeader.center.text = headertext
  15. sheet.oddHeader.center.text = headertext
  16. sheet.evenHeader.center.text = headertext
  17. locationPath = '<path>'
  18. ### Header text (textAdd). Hard set here for code runability
  19. ta = 'LGE Confidential'
  20. # ta = 'LGE Internal Use Only'
  21. for file in glob.glob(locationPath + '\\**\\*.xlsx', recursive=True):
  22. errorFile = os.path.basename(file).split('/')[-1]
  23. print(errorFile[0:2])
  24. if errorFile[0:2] != '~$':
  25. print(file)
  26. ### Open the workbook with Openpyxl
  27. wb = openpyxl.load_workbook(file)
  28. ### Call function to add Header to workbook sheets
  29. addExcelFile(ta, wb)
  30. ### Save updated workbook
  31. wb.save(file)

字符串

展开查看全部

相关问题