繁体   English   中英

openpyxl错误已删除的记录:/xl/worksheets/sheet1.xml中的公式

[英]openpyxl error Removed Records: Formula from /xl/worksheets/sheet1.xml

首先,我是编程方面的新秀。

我正在尝试使用python3将MySQL数据库导出为Excel格式。 我正在使用openpyxl。 现在我在excel中有一个有趣的错误。 如果我运行代码,但是SQL查询很小(大约1000行),那么在查看excel时我不会出现错误,但是当我尝试打开excel时,如果它更大(> 30k行),则会出现错误:

error135840_01.xml在文件'C:\\ Users \\ id022504 \\ PycharmProjects \\ GetMySQLdata \\ Interface planning _mau.xlsx'中检测到错误记录已删除记录:/xl/worksheets/sheet1.xml中的公式

当我使用Open XML SDK打开excel文件时,很有趣,它指出问题出在颜色上:

在此处输入图片说明

在此处输入图片说明

波纹管是代码:

import mysql.connector
import openpyxl
from openpyxl import Workbook
from openpyxl.styles import Border, Side, Font, Alignment
import datetime
import os
from openpyxl.worksheet import Worksheet

mydb = mysql.connector.connect(
  host="10.10.10.10",
  user="user",
  passwd="password"
)

#Funcao para por as colunas com auto size
def auto_column_resize(worksheet):
    for col in worksheet.columns:
         max_length = 0
         column = col[0].column # Get the column name
         for cell in col:
             try: # Necessary to avoid error on empty cells
                 if len(str(cell.value)) > max_length:
                     max_length = len(cell.value)
             except:
                 pass
         adjusted_width = (max_length + 2) * 1.2
         if adjusted_width <= 95:
            worksheet.column_dimensions[column].width = adjusted_width
         else:
             worksheet.column_dimensions[column].width =95
    return worksheet

# definir path
path='C:/Users/'

# definir path para arquivo
path_arquivo='C:/Users/Arquivo/'

#definir tamanho do arquivo
arquivo_file_size = 26





# Abrir ficheiro actual e guardar nos arquivos
current_wb = openpyxl.load_workbook(path+"Interface planning.xlsx")
current_ws = current_wb["Ports allocation"]


if int(datetime.datetime.now().isocalendar()[1]) >9:
    current_wb.save("{0}/Interface planning_{1}{2}.xlsx".format(path_arquivo, str(int(datetime.datetime.now().isocalendar()[0])),str(int(datetime.datetime.now().isocalendar()[1]))))
else:
    #introduce 0 in the week so it can calculate later which file is the oldest
    current_wb.save("{0}/Interface planning_{1}0{2}.xlsx".format(path_arquivo, str(int(datetime.datetime.now().isocalendar()[0])),str(int(datetime.datetime.now().isocalendar()[1]))))

#Abrir SQL e comando
mycursor = mydb.cursor()
mycursor.execute("SELECT hostname, hardware, port_label, ifHighSpeed, ifAdminStatus, ifOperStatus, ifAlias FROM `observium`.`ports` JOIN `observium`.`devices` ON `observium`.`devices`.device_id = `observium`.`ports`.device_id WHERE (port_label LIKE 'xe-%' or port_label LIKE 'et-%' or port_label LIKE 'ge-%' or port_label LIKE '%Ethernet%') and port_label NOT RLIKE '[.][1-9]' ORDER BY hostname, port_label;")


#Meter em tupel os dados recebidos pela base de dados
myresult = mycursor.fetchall()
header = mycursor.column_names

#Criar workbook
new_wb = Workbook()

#Criar worksheet
new_ws = new_wb.active
new_ws.title = "Ports allocation"

############################################## Meter dados SQL em excel ##########################################

#Add header information and formating
new_ws.append(header)

new_ws["H1"].value = "Person assigned"

for format_row in new_ws:
    for i in range(8):
        format_row[i].font = Font(bold=True)

#Add content from SQL to excel
for row in myresult:
    new_ws.append(row)

new_ws.auto_filter.ref = "A:H"

#Verificar o estado da interface e colocar a pessoa responsavel se a interface esta administrativamente e operacionalmente em baixo
for current_ws_row in current_ws:
    if current_ws_row[7].value is not None:
        for new_ws_row in new_ws:
            if (new_ws_row[4].value != "up" or new_ws_row[5].value != "up") and current_ws_row[0].value == new_ws_row[0].value and current_ws_row[2].value == new_ws_row[2].value :
                new_ws_row[7].value= current_ws_row[7].value
                new_ws_row[6].value = current_ws_row[6].value


for format_row in new_ws:
    for i in range(8):
        format_row[i].border = Border(right=Side(style='thin'),)

#Meter o worksheet bonitinho
new_ws = auto_column_resize(new_ws)
new_ws.sheet_view.zoomScale = 85
c=new_ws['D2']
new_ws.freeze_panes = c

wrap_alignment = Alignment(wrap_text=True)
for row in new_ws.iter_rows():
    for cell in row:
        cell.alignment = Alignment(shrink_to_fit=True)

#Salvar workbook
new_wb.save(path+"Interface planning.xlsx")

#remover ficheiro no arquivo

count_files=0
#infinite time
file_to_delete = '299952'

for directory in os.walk(path_arquivo):
    for file in directory[2]:
        count_files = count_files+1
        if  str(file)[-11:-5] < file_to_delete:
            file_to_delete = str(file)[-11:-5]


if count_files > arquivo_file_size:
    os.remove(path_arquivo+'Interface planning_'+file_to_delete+'.xlsx')

我发现了问题,在其中一个单元格中出现了“ =”符号,并以某种方式将excel识别为公式。 为了解决这个问题,我只需要清理“ =”。

import re
try:
    new_ws_row[7].value = re.sub("=", "", new_ws_row[7].value)
except:
    pass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM