加载Excel与几个工作表时出错

我有大量的.xlsx文件从我想要的外部数据库下载。 它有两个工作表,第一个工作表只对数据有一些评论,第二个包含数据。

我已经尝试使用以下两个选项打开Excel电子表格,但他们都给我一个错误。 删除第一个工作表时,该错误消失。 但由于我有> 350个文件,我不想手动删除所有这些工作表。

我试过的代码

from openpyxl import load_workbook wb = load_workbook('/Users/user/Desktop/Data_14.xlsx') 

这给出了错误:

  InvalidFileException: "There is no item named 'xl/styles.xml' in the archive" 

和:

  from xlrd import open_workbook book = open_workbook('/Users/user/Desktop/Data_14.xlsx') 

这会给出非常长的错误信息(KeyError:9)

我认为这个问题是第一个Excel工作表中的公式错误。 工作表中的一个单元格说

 - minimum percentage that must characterise the path from a subject Company up to its Ultimate owner: 50.01% 

但它没有格式化为文本。 执行该单元格会在Excel中给出错误消息。 插入一个“'”使它成为文本,然后我可以用python打开这个文件,这正是我想要做的。

任何想法,我怎么能自动打开Excel文件来解决这个问题?

解:

我已经将脚本命名为delsheet.py,并将其放在一个也包含excel文件的目录中。

  • 我使用的Python 3.4.3和Openpyxl 2.3.0,但这应该适用于Openpyxl 2.0 +
  • 我在运行Yosemite的Mac OS X上。

  • 知道你的版本和设置将是有用的,因为openpyxl可以变化取决于版本的语法。

  • 工作表名称,无论是我看过,或者你没有提到,如果Excel文件中的第一个工作表具有唯一的名称,或者如果他们都是相同的。
  • 如果他们都是一样的,那么这很方便,如果所有的第一张被命名为“Sheet1”,那么这个脚本将按原样工作,这就是你如何写这个问题,所以这就是我写的解决scheme; 如果不同请澄清。 谢谢。

  • 了解脚本:

    1. 首先,脚本存储脚本位置的path,以知道从哪个目录调用并因此被定位。

    2. 脚本从该位置列出文件扩展名为.xlsx的同一目录中的文件,并将它们附加到列表“spreadsheet_list”

    3. 使用for循环并获取列表“spreadsheet_list”中元素的数量可让脚本知道在列表中元素的迭代次数。

      • 循环从列表加载到excel文件中
      • 删除“sheet1”
      • 使用相同的原始文件名保存电子表格。

delsheet.py

 #!/usr/bin/env python3 # Using python 3.4.3 and openpyxl 2.3.0 # Remove the first worksheet from a batch of excel sheets # Import Modules import sys, os, re from openpyxl import Workbook, load_workbook # Create List spreadsheet_list = [] # Script path to the directory it is located. pcwd=os.path.dirname(os.path.abspath(__file__)) # List files in directory by file extension # Specify directory items = os.listdir(pcwd) # Specify extension in "if" loop and append the files in the directory to the "spreadsheet_list" list. for names in items: if names.endswith(".xlsx"): spreadsheet_list.append(names) # Debugging purposes: print out the list of appended excel files in script directory # print(spreadsheet_list) # For loop, using the number of elements in the spreadsheet_list we can determine how long the loop should go for i in range(len(spreadsheet_list)): # print(i) to see that i is = to the number of excel files located in the directory # Load workbook into memory (Opening the Excel file automatically...) wb = load_workbook(spreadsheet_list[int(i)]) ## Store Sheet1 in the workbook as 'ws' ws = wb['Sheet1'] ## Remove the worksheet 'ws' wb.remove_sheet(ws) ## Save the edited excel sheets (with the original name) wb.save(spreadsheet_list[int(i)]) 

请尝试使用此加载项来合并所有第二张图纸。

http://www.rondebruin.nl/win/addins/rdbmerge.htm

或者,运行此脚本以删除所有工作簿中的所有第一张纸。 。 。

 Sub Example() Dim MyPath As String, FilesInPath As String Dim MyFiles() As String, Fnum As Long Dim mybook As Workbook Dim CalcMode As Long Dim sh As Worksheet Dim ErrorYes As Boolean Application.DisplayAlerts = False 'Fill in the path\folder where the files are MyPath = "C:\Users\rshuell001\Desktop\excel\" 'Add a slash at the end if the user forget it If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\" End If 'If there are no Excel files in the folder exit the sub FilesInPath = Dir(MyPath & "*.xl*") If FilesInPath = "" Then MsgBox "No files found" Exit Sub End If 'Fill the array(myFiles)with the list of Excel files in the folder Fnum = 0 Do While FilesInPath <> "" Fnum = Fnum + 1 ReDim Preserve MyFiles(1 To Fnum) MyFiles(Fnum) = FilesInPath FilesInPath = Dir() Loop 'Change ScreenUpdating, Calculation and EnableEvents With Application CalcMode = .Calculation .Calculation = xlCalculationManual .ScreenUpdating = False .EnableEvents = False End With 'Loop through all files in the array(myFiles) If Fnum > 0 Then For Fnum = LBound(MyFiles) To UBound(MyFiles) Set mybook = Nothing On Error Resume Next Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum)) On Error GoTo 0 If Not mybook Is Nothing Then 'Change cell value(s) in one worksheet in mybook On Error Resume Next With mybook.Worksheets(1) ActiveSheet.Delete End With If Err.Number > 0 Then ErrorYes = True Err.Clear 'Close mybook without saving mybook.Close savechanges:=False Else 'Save and close mybook mybook.Close savechanges:=True End If On Error GoTo 0 Else 'Not possible to open the workbook ErrorYes = True End If Next Fnum End If If ErrorYes = True Then MsgBox "There are problems in one or more files, possible problem:" _ & vbNewLine & "protected workbook/sheet or a sheet/range that not exist" End If 'Restore ScreenUpdating, Calculation and EnableEvents With Application .ScreenUpdating = True .EnableEvents = True .Calculation = CalcMode End With Application.DisplayAlerts = True End Sub